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

Average: 5 (1 vote)
By Benjamin Melançon
on 30 Dec
0 comments

Key words and phrases

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

Tags

Description

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
'';
  }
}
?>

 

Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup> <kbd> <s>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.