hook_node_type

5 node.php hook_node_type($op, $info)
6 node.php hook_node_type($op, $info)

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

2 invocations of hook_node_type()

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;
  }
}

Comments

In Drupal 7 the hook has been replaced from a set of new hooks

Drupal 7 doesn't use anymore hook_node_type(), but it uses a family of new hooks; see Converting 6.x modules to 7.x for more details.

How can I prevent

How can I prevent hook_node_type from creating variables?

Login or register to post comments