filterdrupal-7hook-menu

Hook_menu selective menu tab display


I am using the following code to include a custom tab for my node types:

function mymodule_menu(){
    $items['node/%node/register']   =   array(
        'page arguments' => array(1),
        'access arguments'  =>  array('access content'),
        'type'  =>  MENU_LOCAL_TASK,
        'title' =>  'Register',
    );
    return $items;
}

This has the effect of including a register tab for every node type. However, I need to include that tab for only page types and exclude it on all other type like article types etc.

What other directions can I consider?


Solution

  • The easiest way would be to provide your own access callback that checks the node type, e.g.

    function mymodule_menu(){
      $items['node/%node/register'] = array(
        'page arguments' => array(1),
        'access callback' => 'mymodule_node_register_tab_access',
        'access arguments' => array(1),
        'type'  =>  MENU_LOCAL_TASK,
        'title' =>  'Register',
      );
      return $items;
    }
    
    function mymodule_node_register_tab_access($node) {
      $valid_types = array('page');
      return in_array($node->type, $valid_types);
    }