/**
* Implementation of a Drupal action.
* Configurable action to set the comment status
*/
function action_comments_status($op, $edit = array(), &$node) {
    $settings = array(t('Disabled'), t('Read only'), t('Read/Write'));
  switch($op) {
    case 'metadata':
      return array(
        'description' => t('Change comments status'),
        'type' => t('Node'),
        'batchable' => true,
        'configurable' => true,
      );
    // return an HTML config form for the action
    case 'form':
      $form['comment_settings']['comment'] = array(
        '#type' => 'radios',
        '#parents' => array('comment'),
        '#default_value' => $edit['comment_settings'],
        '#options' => $settings,
      );
      return $form;
    // validate the HTML form
    case 'validate':
      return in_array($edit['comment_settings'], $settings);
    // process the HTML form to store configuration
    case 'submit':
      $params = array('comment_settings' => $edit['comment']);
      return $params;
    case 'do':
      $node->comment = $edit['comment_settings'];
      if (!$edit['defer']) {
        node_save($node);
      }
      $message = t('Set comments on node id %id to %status', array('%id' => intval($node->nid), '%status' => $settings[$node->comment]));
      watchdog('action', $message);
      break;
  }
}

Comments

mfredrickson’s picture

This should be added to the Actions Snippets page:

http://drupal.org/node/48737

robertdouglass’s picture

I was rather hoping it would get committed to actions.inc.

jvandyk’s picture

If Drupal core's comment status settings are Disabled|Read only|Read/Write then for consistency with the way we treat other built-in node status settings I think we need three actions here:

Set comment status to Read-Only
Set comment status to Disabled
Set comment status to Read/Write

Peloose’s picture

Can I information (step by step) to add custom action?
regards

pomliane’s picture

Status: Needs review » Closed (won't fix)

This version of Actions is not supported anymore. The issue is closed for this reason.
Please upgrade to a supported version and feel free to reopen the issue on the new version if applicable.

This issue has been automagically closed by a script.

donpwinston’s picture

The most strait forward way is to use hook_node_presave($node) assumming the comment status depends on the content of the node.
( hook_form_alter is crazy)

function <module_name>_node_presave($node) {
  $decision= field_get_items('node', $node, 'field_decision');
  if ($decesion[0]['value'] == 'open')
    $node->comment = COMMENT_NODE_OPEN;
  else if ($issue_status[0]['value'] == 'close')
   $node->comment = COMMENT_NODE_CLOSED;
}

This is for individual nodes.