4.6.x core.php hook_nodeapi(&$node, $op, $teaser = NULL, $page = NULL)
4.7.x core.php hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
5.x core.php hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
6.x core.php hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)

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.

Parameters

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

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

  • "delete": The node is being deleted.
  • "fields": The node is being saved to the database. This hook can be used to declare that additional fields of information should be saved to the node table.
  • "form admin": The node form is about to be displayed to an administrator. This hook can be used to add administrative fields at the top of the form.
  • "form param": The node form is about to be displayed. This hook can be used to change form parameters (e.g. change the encoding type).
  • "form pre": The node form is about to be displayed. This hook can be used to add fields at the top of the form.
  • "form post": The node form was just displayed. This hook can be used to add fields at the bottom of the form.
  • "insert": The node is being created (inserted in the database).
  • "load": The node is about to be loaded from the database. This hook can be used to load additional data at this time.
  • "search result": The node is displayed as a search result. If you want to display extra information with the result, return it.
  • "settings": The "content settings" administration page is being displayed. This hook can be used to add additional columns to the settings table.
  • "update": The node is being updated.
  • "update index": The node is being indexed. If you want additional information to be indexed which is not already visible through nodeapi "view", then you should return it here.
  • "validate": The user has just finished editing the node and is trying to preview or submit it. This hook can be used to check or even modify the node. Errors should be set with form_set_error().
  • "view": The node is about to be presented to the user. The module may change $node->body prior to presentation. This hook will be called after hook_view(), so the module may assume the node is filtered and now contains HTML.

$teaser:

  • For "view", passes in the $teaser parameter from node_view().

$page:

  • For "view", passes in the $page parameter from node_view().

Return value

This varies depending on the operation.

  • The "insert", "update", "delete", and "view" operations have no return value.
  • The "form pre", "form post", and "form admin" operations should return a string containing the HTML to insert in the form.
  • The "fields" operation should return an array of database field names to save.
  • The "settings" operation should return an associative array of HTML snippets to insert for each setting, indexed by setting name.
  • The "form param" operation should return an associative array in the same format as the $param argument of hook_form().
  • The "load" operation should return an object containing extra fields to be merged into the node object.

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().

Related topics

11 functions implement hook_nodeapi()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

book_nodeapi in modules/book.module
Implementation of hook_nodeapi().
comment_nodeapi in modules/comment.module
Implementation of hook_nodeapi().
nodeapi_example_nodeapi in developer/examples/nodeapi_example.module
Implementation of hook_nodeapi().
node_access_example_nodeapi in developer/examples/node_access_example.module
Implementation of hook_nodeapi().
node_invoke_nodeapi in modules/node.module
Invoke a hook_nodeapi() operation in all modules.

... See full list

File

developer/hooks/core.php, line 618
These are the hooks that are invoked by the Drupal core.

Code

function hook_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'fields':
      return array(
        'score',
        'users',
        'votes',
      );
    case 'validate':
      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')) {
        print theme('box', t('Post queued'), t('The post is queued for approval.
        You can check the votes in the <a href="%queue">submission
        queue</a>.', array(
          '%queue' => url('queue'),
        )));
      }
      elseif ($node->moderate) {
        print theme('box', t('Post queued'), t('The post is queued for approval.
        The editors will decide whether it should be published.'));
      }
      else {
        print theme('box', t('Post published'), t('The post is published.'));
      }
      break;
  }
}