Same name and namespace in other branches
  1. 4.6.x developer/hooks/core.php \hook_nodeapi()
  2. 4.7.x developer/hooks/core.php \hook_nodeapi()
  3. 6.x developer/hooks/core.php \hook_nodeapi()

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.
  • "delete revision": The revision of the node is deleted. You can delete data associated with that revision.
  • "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.
  • "prepare": The node is about to be shown on the add/edit form.
  • "search result": The node is displayed as a search result. If you want to display extra information with the result, return it.
  • "print": Prepare a node view for printing. Used for printer-friendly view in book_module
  • "update": The node is being updated.
  • "submit": The node passed validation and will soon be saved. Modules may use this to make changes to the node before it is saved to the database.
  • "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 the node data. Errors should be set with form_set_error().
  • "view": The node content is being assembled before rendering. The module may add elements $node->content prior to rendering. This hook will be called after hook_view(). The format of $node->content is the same as used by Forms API.
  • "alter": the $node->content array has been rendered, so the node body or teaser is filtered and now contains HTML. This op should only be used when text substitution, filtering, or other raw text operations are necessary.
  • "rss item": An RSS feed is generated. The module can return properties to be added to the RSS item generated for this node. See comment_nodeapi() and upload_nodeapi() for examples. The $node passed can also be modified to add or remove contents to the feed item.

$a3:

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

$a4:

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

Return value

This varies depending on the operation.

  • The "submit", "insert", "update", "delete", "print' and "view" operations have no return value.
  • The "load" operation should return an array containing pairs of fields => values 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

12 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/book.module
Implementation of hook_nodeapi().
comment_nodeapi in modules/comment/comment.module
Implementation of hook_nodeapi().
forum_nodeapi in modules/forum/forum.module
Implementation of hook_nodeapi().
menu_nodeapi in modules/menu/menu.module
Implementation of hook_nodeapi().
nodeapi_example_nodeapi in developer/examples/nodeapi_example.module
Implementation of hook_nodeapi().

... See full list

File

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

Code

function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'submit':
      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;
  }
}