• Comment Workflow is enabled for the node type
  • The node\'s comment setting is Read only or Read/Write (Comment Workflow will not alter a Disabled comment setting)
  • A workflow with three or more states (including creation) is assigned to the node type
  • '); case 'admin/settings/comment_workflow': return t('Comment Workflow closes comments if the node state is changed to the last state of a workflow and re-opens comments if the state is changed from the last state. Existing nodes of the !node_types where Comment Workflow is enabled can have the comment setting synchronized to the node\'s current workflow state, which might be helpful after first installing the module or to reset from manual comment setting changes.', array('!node_types' => l('node types', 'admin/content/types'))); } } /** * Implementation of hook_menu(). */ function comment_workflow_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array( 'path' => 'admin/settings/comment_workflow', 'title' => 'Comment Workflow', 'description' => t('Closes comments when last state of workflow is reached.'), 'callback' => 'drupal_get_form', 'callback arguments' => 'comment_workflow_sync', 'type' => MENU_NORMAL_ITEM, 'access' => user_access('administer site configuration'), //do we *really* need yet another permission?? ); } return $items; } /** * Menu callback * * @return * array of form content. */ function comment_workflow_sync() { $form = array(); $form['submit'] = array('#type' => 'submit', '#value' => 'Sync Comment Setting with Workflow States'); return $form; } /** * Sync comment stting with workflow states for enabled node types. * * @param $form_id * The unique string identifying the form. * @param $form_values * An array of values mirroring the values returned by the form * when it is submitted by a user. */ function comment_workflow_sync_submit($form_id, $form_values) { foreach (node_get_types() as $type => $info) { if (!variable_get('comment_workflow_'. $type, 0)) continue; if ($wid = workflow_get_workflow_for_type($type)) { $result = db_fetch_array(db_query('SELECT sid, states FROM {workflow_states} JOIN (SELECT COUNT(sid) as states FROM {workflow_states} WHERE wid=%d GROUP BY wid) as count WHERE wid=%d ORDER BY weight DESC LIMIT 0,1', $wid, $wid)); if ($result['states'] < 3) continue; if ($count = db_num_rows(db_query('SELECT * FROM {node} LEFT JOIN {workflow_node} USING (nid) WHERE node.type="%s" AND node.comment=%d AND workflow_node.sid=%d', $type, COMMENT_NODE_READ_WRITE, $result['sid']))) { db_query('UPDATE {node} LEFT JOIN {workflow_node} USING (nid) SET node.comment=%d WHERE node.type="%s" AND node.comment=%d AND workflow_node.sid=%d', COMMENT_NODE_READ_ONLY, $type, COMMENT_NODE_READ_WRITE, $result['sid']); drupal_set_message(t('Synchronized %count !type where state is last and comments are read/write.', array('%count' => $count, '!type' => $info->name))); $clear_cache = TRUE; } if ($count = db_num_rows(db_query('SELECT * FROM {node} LEFT JOIN {workflow_node} USING (nid) WHERE node.type="%s" AND node.comment=%d AND workflow_node.sid<>%d', $type, COMMENT_NODE_READ_ONLY, $result['sid']))) { db_query('UPDATE {node} LEFT JOIN {workflow_node} USING (nid) SET node.comment=%d WHERE node.type="%s" AND node.comment=%d AND workflow_node.sid<>%d', COMMENT_NODE_READ_WRITE, $type, COMMENT_NODE_READ_ONLY, $result['sid']); drupal_set_message(t('Synchronized %count !type where state is not last and comments are read only.', array('%count' => $count, '!type' => $info->name))); $clear_cache = TRUE; } } } if ($clear_cache) cache_clear_all('*', 'cache', TRUE); drupal_set_message(t('Synchronization complete.')); } /** * Implementation of hook_form_alter(). */ function comment_workflow_form_alter($form_id, &$form) { // Add checkbox to activate per node type if ($form_id == 'node_type_form' && isset($form['identity']['type'])) { // Developers: this will group all comment-related node content type // settings. Copy this code block into your module and we can keep the form // more intuitive until a standard arises. if (!isset($form['comment_options'])) { // create Comment Options fieldset $form['comment_options'] = array( '#type' => 'fieldset', '#title' => t('Comment Options'), '#collapsible' => TRUE, '#weight' => 10 //place below the Workflow fieldset ); // move default Comment setting to fieldset if (isset($form['workflow']['comment'])) { $form['comment_options']['comment'] = $form['workflow']['comment']; $form['comment_options']['comment']['#weight'] = -10; unset($form['workflow']['comment']); } } // end comment option grouping $form['comment_options']['comment_workflow'] = array( '#type' => 'checkbox', '#title' => t('Enable Comment Workflow for this node type'), '#default_value' => variable_get('comment_workflow_'. $form['#node_type']->type, 0), '#options' => array(t('Disabled'), t('Enabled')), '#description' => t('Check this box to close comments if the node state is changed to the last state of a workflow and re-open comments if the state is changed from the last state.'), ); } } /** * Implementation of hook_workflow(). */ function comment_workflow_workflow($op, $old_state, $new_state, $node) { if ($op != 'transition post') return; if ($node->comment == COMMENT_NODE_DISABLED) return; if (!variable_get('comment_workflow_'. $node->type, 0)) return; if ($wid = workflow_get_workflow_for_type($node->type)) { $result = db_fetch_array(db_query('SELECT sid, states FROM {workflow_states} JOIN (SELECT COUNT(sid) as states FROM {workflow_states} WHERE wid=%d GROUP BY wid) as count WHERE wid=%d ORDER BY weight DESC LIMIT 0,1', $wid, $wid)); if ($result['states'] < 3) return; if ($new_state == $result['sid']) { //last state if ($node->comment == COMMENT_NODE_READ_WRITE) { _comment_workflow_save($node, COMMENT_NODE_READ_ONLY); } } else { //not last state if ($node->comment == COMMENT_NODE_READ_ONLY) { _comment_workflow_save($node, COMMENT_NODE_READ_WRITE); } } } } /** * Internal function. */ function _comment_workflow_save(&$node, $setting = COMMENT_NODE_READ_ONLY) { $node->comment = $setting; node_save($node); $message = t('Comments have been !action on %title', array( '!action' => ($setting == COMMENT_NODE_READ_ONLY) ? t('closed') : t('re-opened'), '%title' => $node->title )); drupal_set_message($message); watchdog('comment workflow', $message, WATCHDOG_NOTICE); }