Disable comments on node_type

Public

Get raw version
php
  1. /**
  2.  * Disable comments on node_type
  3.  */
  4. function hook_update_N(&$sandbox) {
  5. $content_type = 'node_type';
  6.  
  7. // Initialize batch.
  8. if (!isset($sandbox['total'])) {
  9. $query = db_select('node');
  10. $query->addExpression('COUNT(*)');
  11. $query->condition('type', $content_type);
  12.  
  13. $sandbox['total'] = $query->execute()->fetchField();
  14. $sandbox['progress'] = 0;
  15.  
  16. if (empty($sandbox['total'])) {
  17. $sandbox['#finished'] = 1;
  18. return t('No %type nodes exist in database.', array('%type' => $content_type));
  19. }
  20. }
  21.  
  22. // Get and update nodes.
  23. $nids = db_select('node')
  24. ->fields('node', array('nid'))
  25. ->condition('type', $content_type)
  26. ->range(0, 10)
  27. ->execute()
  28. ->fetchCol();
  29.  
  30. if (!empty($nids)) {
  31. $nodes = node_load_multiple($nids, NULL, TRUE);
  32.  
  33. foreach ($nodes as $node) {
  34. $node->comment = 1; // I set comments as 1 where value of 2 enables the comments.
  35. node_save($node); // Re-save the node.
  36. }
  37. }
  38.  
  39. // Increment & check progress.
  40. $sandbox['progress'] += count($nids);
  41. if (empty($nids) || $sandbox['progress'] >= $sandbox['total']) {
  42. $sandbox['#finished'] = 1;
  43. return t('Updated @count nodes.', array('@count' => $sandbox['progress']));
  44. }
  45. else {
  46. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['total'];
  47. }
  48. }