Same filename and directory in other branches
  1. 4.7.x developer/hooks/node.php
  2. 5.x developer/hooks/node.php
  3. 6.x developer/hooks/node.php

These hooks are defined by node modules, modules that define a new kind of node.

If you don't need to make a new node type but rather extend the existing ones, you should instead investigate using hook_nodeapi().

Node hooks are typically called by node.module using node_invoke().

File

developer/hooks/node.php
View source
<?php

/**
 * @file
 * These hooks are defined by node modules, modules that define a new kind
 * of node.
 *
 * If you don't need to make a new node type but rather extend the existing
 * ones, you should instead investigate using hook_nodeapi().
 *
 * Node hooks are typically called by node.module using node_invoke().
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
 * Define access restrictions.
 *
 * This hook allows node modules to limit access to the node types they
 * define.
 *
 * @param $op
 *   The operation to be performed. Possible values:
 *   - "create"
 *   - "delete"
 *   - "update"
 *   - "view"
 * @param $node
 *   The node on which the operation is to be performed, or, if it does
 *   not yet exist, the type of node to be created.
 * @return
 *   TRUE if the operation may be performed; FALSE if the operation may not be
 *   returned; NULL to not override the settings in the node_access table.
 *
 * The administrative account (user ID #1) always passes any access check,
 * so this hook is not called in that case. If this hook is not defined for
 * a node type, all access checks will fail, so only the administrator will
 * be able to see content of that type. However, users with the "administer
 * nodes" permission may always view and edit content through the
 * administrative interface.
 *
 * For a detailed usage example, see node_example.module.
 *
 * @ingroup node_access
 */
function hook_access($op, $node) {
  global $user;
  if ($op == 'create') {
    return user_access('create stories');
  }
  if ($op == 'update' || $op == 'delete') {
    if (user_access('edit own stories') && $user->uid == $node->uid) {
      return TRUE;
    }
  }
}

/**
 * Respond to node deletion.
 *
 * This is a hook used by node modules. It is called to allow the module
 * to take action when a node is being deleted from the database by, for
 * example, deleting information from related tables.
 *
 * @param &$node
 *   The node being deleted.
 * @return
 *   None.
 *
 * To take action when nodes of any type are deleted (not just nodes of
 * the type defined by this module), use hook_nodeapi() instead.
 *
 * For a detailed usage example, see node_example.module.
 */
function hook_delete(&$node) {
  db_query('DELETE FROM {mytable} WHERE nid = %d', $node->nid);
}

/**
 * Display a node editing form.
 *
 * This hook, implemented by node modules, is called to retrieve the form
 * that is displayed when one attempts to "create" an item. This form is
 * displayed at the URI http://www.yoursite.com/?q=node/add/nodetype.
 *
 * @param &$node
 *   The node being added or edited.
 * @param &$param
 *   The hook can set this variable to an associative array of attributes
 *   to add to the enclosing \<form\> tag.
 * @return
 *   A string containing HTML defining form elements to be displayed in
 *   the node edit form.
 *
 * The "Title" field, submit and preview buttons, and administrative
 * accoutrements are displayed automatically by node.module. This hook
 * needs to return only the body text area, taxonomy controls, and fields
 * specific to the node type.
 *
 * For a detailed usage example, see node_example.module.
 */
function hook_form(&$node, &$param) {
  if (function_exists('taxonomy_node_form')) {
    $output = implode('', taxonomy_node_form('example', $node));
  }
  $output .= form_textfield(t('Custom field'), 'field1', $node->field1, 60, 127);
  $output .= form_select(t('Select box'), 'selectbox', $node->selectbox, array(
    1 => 'Option A',
    2 => 'Option B',
    3 => 'Option C',
  ), t('Please choose an option.'));
  return $output;
}

/**
 * Respond to node insertion.
 *
 * This is a hook used by node modules. It is called to allow the module
 * to take action when a new node is being inserted in the database by,
 * for example, inserting information into related tables.
 *
 * @param $node
 *   The node being inserted.
 * @return
 *   None.
 *
 * To take action when nodes of any type are inserted (not just nodes of
 * the type(s) defined by this module), use hook_nodeapi() instead.
 *
 * For a detailed usage example, see node_example.module.
 */
function hook_insert($node) {
  db_query("INSERT INTO {mytable} (nid, extra)\n    VALUES (%d, '%s')", $node->nid, $node->extra);
}

/**
 * Load node-type-specific information.
 *
 * This is a hook used by node modules. It is called to allow the module
 * a chance to load extra information that it stores about a node.
 *
 * @param $node
 *   The node being loaded. At call time, node.module has already loaded
 *   the basic information about the node, such as its node ID (nid),
 *   title, and body.
 * @return
 *   An object containing properties of the node being loaded. This will
 *   be merged with the passed-in $node to result in an object containing
 *   both sets of properties.
 *
 * For a detailed usage example, see node_example.module.
 */
function hook_load($node) {
  $additions = db_fetch_object(db_query('SELECT * FROM {mytable} WHERE nid = %s', $node->nid));
  return $additions;
}

/**
 * Define the human-readable name of a node type.
 *
 * This is a hook used by node modules. This hook is required of modules
 * that define a node type. It is called to define the name of a type of
 * node.
 *
 * @param $node
 *   Either the node type as a string, or a node object (in which case
 *   $node->type contains the node type as a string).
 * @return
 *   A string containing the node type name.
 *
 * The most basic node module consists of only this hook. If the module
 * defines only one type of node, then the function can consist of a
 * single line returning the translated node type name.
 *
 * For a detailed usage example, see node_example.module.
 */
function hook_node_name($node) {
  switch (is_string($node) ? $node : $node->type) {
    case 'project-issue':
      return t('issue');
    case 'project-project':
      return t('project');
  }
}

/**
 * Define multiple node types.
 *
 * This is a hook used by node modules. This hook is only needed if a
 * module needs to define more than one node type.
 *
 * @return
 *   An array of node types defined by the module.
 *
 * To prevent namespace conflicts, each node type defined by a module
 * should be prefixed by the name of the module and a dash.
 */
function hook_node_types() {
  return array(
    'project-issue',
    'project-project',
  );
}

/**
 * Respond to node updating.
 *
 * This is a hook used by node modules. It is called to allow the module
 * to take action when an edited node is being updated in the database by,
 * for example, updating information in related tables.
 *
 * @param $node
 *   The node being updated.
 * @return
 *   None.
 *
 * To take action when nodes of any type are updated (not just nodes of
 * the type(s) defined by this module), use hook_nodeapi() instead.
 *
 * For a detailed usage example, see node_example.module.
 */
function hook_update($node) {
  db_query("UPDATE {mytable} SET extra = '%s' WHERE nid = %d", $node->extra, $node->nid);
}

/**
 * Verify a node editing form.
 *
 * This is a hook used by node modules. It is called to allow the module
 * to verify that the node is in a format valid to post to the site. It
 * can also be used to make changes to the node before submission, such
 * as node-type-specific formatting. Errors should be set with
 * form_set_error().
 *
 * @param &$node
 *   The node to be validated.
 * @return
 *   None.
 *
 * To validate nodes of all types (not just nodes of the type(s) defined by
 * this module), use hook_nodeapi() instead.
 *
 * For a detailed usage example, see node_example.module.
 */
function hook_validate(&$node) {
  if ($node) {
    if ($node->end && $node->start) {
      if ($node->start > $node->end) {
        form_set_error('time', t('An event may not end before it starts.'));
      }
    }
  }
}

/**
 * Display a node.
 *
 * This is a hook used by node modules. It allows a module to define a
 * custom method of displaying its nodes, usually by displaying extra
 * information particular to that node type.
 *
 * @param &$node
 *   The node to be displayed.
 * @param $teaser
 *   Whether we are to generate a "teaser" or summary of the node, rather than
 *   display the whole thing.
 * @param $page
 *   Whether the node is being displayed as a standalone page. If this is
 *   TRUE, the node title should not be displayed, as it will be printed
 *   automatically by the theme system. Also, the module may choose to alter
 *   the default breadcrumb trail in this case.
 * @return
 *   None. The passed-by-reference $node parameter should be modified as
 *   necessary so it can be properly presented by theme('node', $node). This
 *   means, for instance, that content should be passed through the filter
 *   system by calling check_output() on appropriate fields or by sending the
 *   node through node_prepare().
 *
 * For a detailed usage example, see node_example.module.
 */
function hook_view(&$node, $teaser = FALSE, $page = FALSE) {
  if ($page) {
    $breadcrumb = array();
    $breadcrumb[] = array(
      'path' => 'example',
      'title' => t('example'),
    );
    $breadcrumb[] = array(
      'path' => 'example/' . $node->field1,
      'title' => t('%category', array(
        '%category' => $node->field1,
      )),
    );
    $breadcrumb[] = array(
      'path' => 'node/' . $node->nid,
    );
    menu_set_location($breadcrumb);
  }
  $node = node_prepare($node, $teaser);
}

/**
 * @} End of "addtogroup hooks".
 */

Functions

Namesort descending Description
hook_access Define access restrictions.
hook_delete Respond to node deletion.
hook_form Display a node editing form.
hook_insert Respond to node insertion.
hook_load Load node-type-specific information.
hook_node_name Define the human-readable name of a node type.
hook_node_types Define multiple node types.
hook_update Respond to node updating.
hook_validate Verify a node editing form.
hook_view Display a node.