User login

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

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

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

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • 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>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.

More information about formatting options

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