How to create sub tab in Drupal 7

What is Sub tabs.

Sub tabs are a menu style which looks like tab.

How it Looks ???

Got It…. But how to implement this ??

Let us assume i have uploaded a .xls file and i want to give an option for user to download this file in different format. I will create separate sub tabs for this like below:

I am going to give an example to create sub-tab.

Create a module for this and call hook_menu() first and in the hook_menu() write below lines:

function hook_menu() {
$items['node/%yourmodule/download/csv'] = array(
    'title' => t('Download CSV'),
    'page callback' => 'change_formater',
    'page arguments' => array(1,2),
    'access arguments' => array(1),
    'file' => 'your_file.inc',
    'access callback'=>'check_downlodable_callback',
    'type' => MENU_LOCAL_TASK,
  );  
}

Description of every line:

  • In first line we are reserving a slug. Slug is nothing but a URL. On the click of this tab the URL will get called.
  • In the Second line we are giving title(lebel) for this sub tab. Wee always include any printable statement with t(); method.
  • In the Third line is a page callback. Page callback is nothing but a method name that you have written for your purpose.
  • Page argument: We are sending page argument to that method. It starts from 0index. so index we have download and on 2nd we have csv. So in your callback method change_formater() you will have have to receive two argument.
  • Wee are checking that this sub tab is only show on the selected content type. SO we have passed access argument =array (1). in our access callback we will get entire node. So we can get node type from there and bind a condition. So that only selected content type will have this this subtab.
  • Next is file so Best practice is to create your .inc file and keep your logic in this file. Same as keep your all download conversion logic in this .inc file.
  • access callback: Here we define a method name. In this method we will check the accessibility of this tab.
  • Type is MENU_LOCAL_TASK. It will generate a subtab.

Here is the access callback method so that you can understand it more clear.

function check_downlodable_callback($node){
  if ($node->type=='resource')
    return true;
  else
    return false;
}

As i have said that passing 1 in array for access argument. We will get the entire node in method. I have checked that if node type is resource only then this subtab will display.

Hope it will help You. Will share some more help full stuff.

2 Comments

Leave a Comment.