We offer affordable conversion services to convert from PSD to Drupal.
Submit your own design composition to us to start building your Drupal site now.
Submit your brief for custom modules that you'd like us to build.

Drupal creating a third level tabs

Article by duckz
created at 26. Sep, 2011

We all know about Drupal primary and secondary tabs, but now you need to have a "third level" tabs. Is it even possible with Drupal 6?

What Drupal doesn't exactly let its user know that with the current tabs menu system it is very much possible to have "third level" tabs, or even to have more than 3 level tabbing system.

The key to it is retrieving the menu parents and fetching the children of the parent regardless of what level is the parent or the children.

consider this menu entry :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function yourmodulename_menu() {
  $items = array();
  $items['user/%user/level1'] = array(
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
 
  $items['user/%user/level1/level2'] = array(
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  $items['user/%user/level1/level2'] = array(
    'type' => MENU_LOCAL_TASK,
    'weight' => 0,
  );
  $items['user/%user/level1/level2/level3'] = array(
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  $items['user/%user/level1/level2/level3'] = array(
    'type' => MENU_LOCAL_TASK,
    'weight' => 0,
  );
  return $items;
}

normally you cannot view beyond the level2 menu, but utilizing some coding like this will display the level3 menu entry

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function yourmodulename_local_tasks() {
  $router_item = menu_get_item();
  if (!$router_item || !$router_item['access']) {
    return '';
  }
  // send the path if there is no arg 4 (we assume this is already the tab parent
  // else send the tab parent url
  $router = (arg(4)) ?  $router_item['tab_parent'] : $router_item['path'];
 
 
  // set the default for the first parent
  if (arg(2) == 'level1' && !arg(3)) {
    $router .= '/level2';
    $active = TRUE; // quick fix for the first default set active
    return FALSE;
  }
 
  if (arg(3) == 'level3' && !arg(4)) {
    $active = TRUE;
  }
 
  if (!arg(4)) {
    $active = TRUE; // quick fix for the first second level default set active
  }
  // Get all the third tabs
  $result = db_query("SELECT * FROM {menu_router} WHERE tab_parent = '%s' ORDER BY weight, title", $router);
  $map = arg();
  $current_last_path = array_pop($map);
 
  // build the third tabs
  $output = '';
  $tabitem = '';
  while ($item = db_fetch_array($result)) {
    $default_active = FALSE;
    _menu_translate($item, $map, TRUE);
    $linkcheck = explode('/', $item['href']);
 
    $item['localized_options'] = array();
 
    // build the query if there is any
    if (is_numeric($_REQUEST['job'])) {
      $item['localized_options']['query'] = array('job' => $_REQUEST['job']);
    }
 
    if ($active) {
      $item['localized_options']['attributes'] = array('class' => 'active');
      $default_active = TRUE;
      // reset the $active trap since it can only be used once
      $active = FALSE;
    }
    $current_url = array_pop($linkcheck);
    if ($current_url == $current_last_path) {
      $default_active = TRUE;
    }
    $link = theme('menu_item_link', $item);
 
    $tabitem .= theme('menu_local_task', $link, $default_active);
  }
  if (!empty($tabitem)) {
    $output = '<ul class="whateverclass">' . $tabitem . '</ul>';
  }
  return $output;
}

Offcourse the code is not a copy & paste code, you may have to adjust it to your situation. but in general calling the function above will render the level3 tabs for you to use.

Hope this is helpful

1 comment
Barinder Singh (not verified) say:
Wed, 02/15/2012 - 07:06
Barinder Singh's picture

Why not to use : http://api.drupal.org/api/drupal/includes!menu.inc/function/menu_local_tasks/6

menu_local_tasks(2);

Post new comment

The content of this field is kept private and will not be shown publicly.
Image CAPTCHA
Enter the characters shown in the image.
To prevent automated spam submissions leave this field empty.