Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

How to enable comments on multiple nodes

Parent Feed: 

Today I had a bunch of old nodes from a migrated d6 site that did not have comments enabled, although the content type had comments enabled. This could also be the case if you have created a bunch of nodes, and all of sudden change your mind and want to enable comments on them anyway. One could always edit each one of them and turn on the comments, but that just can't be the only way, I thought, and did some research.

At first I didn't find any out-of-the-box way to enable comments for multiple nodes. Programatically enabling comments for all nodes by looping through nodes and enabling comments in a simple script could always be done, but my solution was even simpler. I used the module Views bulk operations. The easiest way was if VBO had an action already created for enabling comments, but as that wasn’t the case, it is really not a long snippet to execute on each row. Here is the steps I used to achieve what I wanted.

  1. Downloaded and enabled views bulk operations
  2. Created a view displaying all nodes of the content type I wanted to enable comments on
  3. Added a VBO field
  4. Enabled the Execute PHP action for this field, and also the “select all nodes” checkbox
  5. Added a page display
  6. Viewed the view page, selected all nodes and executed the PHP action.
  7. Entered the following snippet as action
$entity->comment = 2;
node_save($entity);

Done!

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web