Skip to main content

Display menu or tab according to nodetype in Drupal

Sometime your wan’t to display menus and tabs in drupal as per the nodetype. For example, you want “Images” tab in case of nodetype “Hotel” but not want this when story or page nodetype is viewed. In drupal 6.x, the menu system is changed a lot.

<?php
$items['node/%hotel_node/images'] = array(
	‘title’ => ‘Images’,
	‘type’ => MENU_LOCAL_TASK,
	‘weight’ => -10,
	‘page callback’ => ’show_tab_page’,
 ‘page arguments’ => array(1,’images’),
 ‘access callback’ => TRUE,
 ‘access arguments’ => array(1),
);
 
function hotel_node_load($nid) {
	if (is_numeric($nid)) {
		$node = node_load($nid);
 if ($node->type == ‘hotel’) {
  return $node;
 }
 }
	return FALSE;
}
?>

Here, when the menu system is build its call “hotel_node_load()” function with “$nid” as function parameter. We will check the type of node and if the current node type is “hotel” we will return currently loaded node. Otherwise we will return FALSE. This way only node having type “hotel” will display “Images” tab.

Reference: http://www.akchauhan.com/display-menu-according-to-nodetype-in-drupal/