User login

Make a Drupal action that can be used with views node operations (disable comments on node exampe)

First of all,to use this action, install http://drupal.org/project/views_bulk_operations

Next, put code like the below in any module you happen to have handy. In our case we had the agaric glue module for AgaricDesign.com. The .info file is attached, because all you need is a barebones .info and a .module file that contains something like the below. For instance, for us that means the agaric.info file attached and an agaric.module file that contains just the below PHP (with an opening <?php tag but without the closing ?> tag, per Drupal coding standards).

Note that the action API is very odd in that, contrary to most Drupal hooks which have a standard ending (and the first part of the function name is always the module name), for actions the first part of the function name is action_ followed by something that describes your action.

This creates quite the possibility of function namespace collision, so be extra sure that the action you need isn't already implemented.

<?php
/**
 * Implementation of a Drupal action.
 * Disable comments for a node.
 *
 */
function action_node_disable_comments($op, $edit = array(), &$node) {
  switch($op) {
    case 'metadata':
      return array(
        'description' => t('Disable comments for node'),
        'type' => t('Node'),
        'batchable' => true,
        'configurable' => false,
      );

    case 'do':
      $node->comment = '0';
      if (!$edit['defer']) {
        node_save($node);
      }
      watchdog('action', t('Disabed comments on node id %id', array('%id' => intval($node->nid))));
      break;

    // process the HTML form to store configuration
    case 'submit':
      return '';
  }
}
?>

Resolution

Searched words: 
actions module views node operations custom action to disable comments on a node

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.