Contributions API

Calling all Drupal developers!

Help us get this on the first page of Digg. DIGG NOW!

Modules in 6

hook_nodeapi

Definition

hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
documentation/hooks/core.php, line 1317

Description

Act on nodes defined by other modules.

Despite what its name might make you think, hook_nodeapi() is not reserved for node modules. On the contrary, it allows modules to react to actions affecting all kinds of nodes, regardless of whether that module defined the node.

It is common to find hook_nodeapi() used in conjunction with hook_form_alter(). Modules use hook_form_alter() to place additional form elements onto the node edit form, and hook_nodeapi() is used to read and write those values to and from the database.

If you are writing a node module, do not use this hook to perform actions on your type of node alone. Instead, use the hooks set aside for node modules, such as hook_insert() and hook_form(). That said, for some operations, such as "delete revision" or "rss item" there is no corresponding hook so even the module defining the node will need to implement hook_nodeapi().

Parameters

&$node The node the action is being performed on.

$op What kind of action is being performed. Possible values:

$a3 $a4

Return value

This varies depending on the operation.

Related topics

Namesort iconDescription
HooksAllow modules to interact with the Drupal core.

Code

<?php
function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'presave':
      if ($node->nid && $node->moderate) {
        // Reset votes when node is updated:
        $node->score = 0;
        $node->users = '';
        $node->votes = 0;
      }
      break;
    case 'insert':
    case 'update':
      if ($node->moderate && user_access('access submission queue')) {
        drupal_set_message(t('The post is queued for approval'));
      }
      elseif ($node->moderate) {
        drupal_set_message(t('The post is queued for approval. The editors will decide whether it should be published.'));
      }
      break;
    case 'view':
      $node->content['my_additional_field'] = array(
        '#value' => theme('mymodule_my_additional_field', $additional_field),
        '#weight' => 10,
      );
      break;
  }
}
?>