Hi there, everyone. I'm a somewhat new Drupal developer, with not as much experience as many of the hardcore developers here, but I appreciate and understand--at least basically--how Drupal works.

I was searching for a way to add tabs to one content type, but not another. I hadn't found a way to do that in google or the Drupal forums, but I knew it was possible. The method I found that worked relied on the access aspect of the menu, which in retrospect, makes sense.

Suppose we have the following menu item:


$items['node/%node/foo'] = array(
  'title' => 'Foo',
  'page callback' => 'foo',
  'type' => MENU_LOCAL_TASK,
);

But, we only want a tab "foo" to show up on node type "bar." We use 'access callback' and 'access arguments' to fix this:


$items['node/%node/foo'] = array(
  'title' => 'Foo',
  'page callback' => 'foo',
  'access callback' => 'foo_access'
  'access arguments' => array(1),
  'type' => MENU_LOCAL_TASK,
);

function foo_access($node){
  
  if($node->type == 'bar'){
    return user_access('administer nodes'); // Return the user access for this node if this node is of the type we're looking for
  }
  else{
    return FALSE; //It's not the type we want to display this tab for!
  }

}

Please note, foo_access receives a node object, passed to it from 'access arguments' => array(1). This is because in the 6.x menu API, %node loads a node object in that wildcard space.

So, using 'access callback' and 'access arguments,' we can customize which local tasks we want to display for specific node types.

Comments

bgogoi’s picture

Yes, I also need a Node-specific menu. when we visit a node, its Menu will appear in a block, and when we move to another node, this disappears and comes the another menu that is associated with the next node.

There is a cck_Node_Menu: http://drupal.org/project/cck_nodemenu

But its is still in 5.x Beta.

someone may please check this