How to add a tab to the node in Drupal

And another Drupal tip for today: "How to add a tab to the node in Drupal":

Put this snippet in hook_menu of your new module:

  1. $items['node/%node/new_tab'] = array(
  2. 'title' => 'New Tab',
  3. 'page callback' => 'mycallback',
  4. 'page arguments' => array(1),
  5. 'access callback' => TRUE,
  6. 'type' => MENU_LOCAL_TASK
  7. );

This tab is going to be shown on any node type and without access restrictions, however, if you need to specify a special node type, 'custom_node' for example, do the following: put this snippet in hook_menu of your new module:

  1. $items['node/%custom_node/new_tab'] = array(
  2. 'title' => 'New Tab',
  3. 'page callback' => 'mycallback',
  4. 'page arguments' => array(1),
  5. 'access callback' => TRUE,
  6. 'type' => MENU_LOCAL_TASK
  7. )

And create a function:

  1. function custom_node_load($arg) {
  2. $node = node_load($arg);
  3. if ($node->type == 'custom_node') return $node;
  4. return FALSE;
  5. }

Hope this helps!