How to add a tab to a content-type or a user page

To add a tab to a drupal node or a page you need to use hook_menu. Let us start by creating a skeleton module, you can find more info on how to create a Drupal 6 module here. Once of you have the basic structure of a module i.e. info file and module file in place, to add a tab on a particular content-type you need do to the following:

1. Letting Drupal know about the tab

/**  * Implementation of hook_menu().  */ 
function my_module_menu() {       // adding a tab my_tab  
 /**   
* Parts of url could be variable, which is indicated   
* by using '%' sign. Which means a tab would be rendered at   
* node/1/my_tab or node/13/my_tab etc.   */     
/**    
* Instructing Drupal to take the following configured actions    
* if a request url matches the key of the array $items    
*/    
$items['node/%node/my_tab'] = array(     'title' => 'My Tab',      
/**     
* Function that needs to be call to generate content for     
* the mentioned path like node/2/my_tab     
*/     
'page callback' => 'generate_my_tab',     
 /**     
* In the url above      
* index 0 is the string 'node'     
* index 1 is the node id 1 or 13 or whatever the node at     
* index 3 is the string 'my_tab'     
* array(1) as a page argument says, the first argument     
* argument to the page callback function is index 1     
* /     
'page arguments' => array(1),      
/**     
* access callback is a function or boolean expression     
* that determines who gets to see this tab.     
*/     
'access callback' => 'my_tab_access',      
/**     
* access arguments are the arguments passed to the access callback     
* and we are passing the same argument we passed to      
* page arguments here.     
'access arguments'=> array(1),          
/**     
*  type MENU_LOCAL_TASK are rendered as tabs by default.      
*/     
'type' => MENU_LOCAL_TASK,      
/**     
* weight determines the order in which the tabs are     
* displayed.    
*/      
'weight' => 10,   );
return $items; 
}  

2. Generating content that will show up in the tab.

Here is a simple page callback

/**

* Implementing the page callback function

* for the my_tab

*/

function generate_my_tab($node){

// printing the node object

  return print_r($node,true);

}

 

To find more on how nid is converted to a node object magically. You will need to look at /includes/menu.inc

3. Making sure that this tab appears only on certain node types.

For example: only pages.

/**

* Implementing an access callback function

* for my_tab 

**/

function my_tab_access($node){

  //gives access to the tab only if you are

  //visiting a page node

  if($node->type == "page"){

    return true;

  }

  return false;

}

 

Tagged as :