4

How do I create secondary local tasks with hook_menu in Drupal 7?

For instance, if I wanted to add a new tab to a node, that presented multiple tasks beneath it?

  • View
  • Edit
  • Update
    • Price
    • Photo
    • Person
6

You should use a combination of the MENU_LOCAL_TASK and MENU_DEFAULT_LOCAL_TASK types:

 $items['node/%node/update'] = array(
   'title' => 'Update',
   'page callback' => 'price_callback_function',
   'access arguments' => array('access args'),
   'type' => MENU_LOCAL_TASK // This adds the 'Update' tab to the node path
 );

 $items['node/%node/update/price'] = array(
   'title' => 'Price',
   'type' => MENU_DEFAULT_LOCAL_TASK,
   'weight' => -10
 );

 $items['node/%node/update/photo'] = array(
   'title' => 'Photo',
   'page callback' => 'callback_function',
   'access arguments' => array('access args'),
   'type' => MENU_LOCAL_TASK // This adds another tab under 'Update'
 );

// etc...

You can control the order of the tab using the weight key in your menu item.

Don't forget to clear Drupal's cache once you add this to hook_menu()

  • Ok, so this cascades into secondary tabs? I thought I had tried this but didn't see a result. I will try again. – Kevin Jan 27 '12 at 20:00
  • @Kevin I've updated the answer to something that will probably work a bit better, it sets the 'Update' tab to use the callback for the price page (as Price will be the default tab anyway) and actually creates that default tab with the second item. Descendant paths inherit the attributes of their parent(s) so you don't need to set the callback etc for the second item – Clive Jan 27 '12 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.