Same name and namespace in other branches
  1. 5.x developer/hooks/node.php \hook_node_type()

Act on node type changes.

This hook allows modules to take action when a node type is modified.

Parameters

$op: What is being done to $info. Possible values:

  • "delete"
  • "insert"
  • "update"

Most modules will not need the update or insert operations, because form fields you add to the node type editing form will be automatically saved as variables. For instance, if you add a field called 'my_module_foo' to content type 'machine_name', its value will be saved as variable 'my_module_foo_machine_name' (and if the machine name is updated, the variables will be updated appropriately).

$info: The node type object on which $op is being performed.

Related topics

6 functions implement hook_node_type()

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

book_node_type in modules/book/book.module
Implementation of hook_node_type().
comment_node_type in modules/comment/comment.module
Implementation of hook_node_type().
locale_node_type in modules/locale/locale.module
Implementation of hook_node_type().
node_node_type in modules/node/content_types.inc
Implementation of hook_node_type().
system_node_type in modules/system/system.module
Implementation of hook_node_type().

... See full list

2 invocations of hook_node_type()
node_type_delete in modules/node/node.module
Deletes a node type from the database.
node_type_save in modules/node/node.module
Saves a node type to the database.

File

developer/hooks/node.php, line 97
These hooks are defined by node modules, modules that define a new kind of node.

Code

function hook_node_type($op, $info) {
  switch ($op) {
    case 'delete':

      // Example from comment.module.
      variable_del('comment_' . $info->type);
      break;
    case 'update':

      // Here is an example where you do need an update operation (from the
      // book module), because the simple default case doesn't cover what needs
      // to be done.
      if (!empty($info->old_type) && $info->old_type != $info->type) {

        // Update the list of node types that are allowed to be added to books.
        $allowed_types = variable_get('book_allowed_types', array(
          'book',
        ));
        $key = array_search($info->old_type, $allowed_types);
        if ($key !== FALSE) {
          $allowed_types[$info->type] = $allowed_types[$key] ? $info->type : 0;
          unset($allowed_types[$key]);
          variable_set('book_allowed_types', $allowed_types);
        }

        // Update the setting for the "Add child page" link.
        if (variable_get('book_child_type', 'book') == $info->old_type) {
          variable_set('book_child_type', $info->type);
        }
      }
      break;
  }
}