Community Documentation

menu_set_active_trail

6 menu.inc menu_set_active_trail($new_trail = NULL)
7 menu.inc menu_set_active_trail($new_trail = NULL)
8 menu.inc menu_set_active_trail($new_trail = NULL)

Sets or gets the active trail (path to root menu root) of the current page.

Parameters

$new_trail: Menu trail to set, or NULL to use previously-set or calculated trail. If supplying a trail, use the same format as the return value (see below).

Return value

Path to menu root of the current page, as an array of menu link items, starting with the site's home page. Each link item is an associative array with the following components:

  • title: Title of the item.
  • href: Drupal path of the item.
  • localized_options: Options for passing into the l() function.
  • type: A menu type constant, such as MENU_DEFAULT_LOCAL_TASK, or 0 to indicate it's not really in the menu (used for the home page item).

If $new_trail is supplied, the value is saved in a static variable and returned. If $new_trail is not supplied, and there is a saved value from a previous call, the saved value is returned. If $new_trail is not supplied and there is no saved value, the path to the current page is calculated, saved as the static value, and returned.

Related topics

▾ 2 functions call menu_set_active_trail()

book_nodeapi in modules/book/book.module
Implementation of hook_nodeapi().
menu_get_active_trail in includes/menu.inc
Gets the active trail (path to root menu root) of the current page.

File

includes/menu.inc, line 1530
API for the Drupal menu system.

Code

<?php
function menu_set_active_trail($new_trail = NULL) {
  static $trail;

  if (isset($new_trail)) {
    $trail = $new_trail;
  }
  elseif (!isset($trail)) {
    $trail = array();
    $trail[] = array(
      'title' => t('Home'),
      'href' => '<front>',
      'localized_options' => array(),
      'type' => 0,
    );
    $item = menu_get_item();

    // Check whether the current item is a local task (displayed as a tab).
    if ($item['tab_parent']) {
      // The title of a local task is used for the tab, never the page title.
      // Thus, replace it with the item corresponding to the root path to get
      // the relevant href and title.  For example, the menu item corresponding
      // to 'admin' is used when on the 'By module' tab at 'admin/by-module'.
      $parts = explode('/', $item['tab_root']);
      $args = arg();
      // Replace wildcards in the root path using the current path.
      foreach ($parts as $index => $part) {
        if ($part == '%') {
          $parts[$index] = $args[$index];
        }
      }
      // Retrieve the menu item using the root path after wildcard replacement.
      $root_item = menu_get_item(implode('/', $parts));
      if ($root_item && $root_item['access']) {
        $item = $root_item;
      }
    }

    $tree = menu_tree_page_data(menu_get_active_menu_name());
    list($key, $curr) = each($tree);

    while ($curr) {
      // Terminate the loop when we find the current path in the active trail.
      if ($curr['link']['href'] == $item['href']) {
        $trail[] = $curr['link'];
        $curr = FALSE;
      }
      else {
        // Add the link if it's in the active trail, then move to the link below.
        if ($curr['link']['in_active_trail']) {
          $trail[] = $curr['link'];
          $tree = $curr['below'] ? $curr['below'] : array();
        }
        list($key, $curr) = each($tree);
      }
    }
    // Make sure the current page is in the trail (needed for the page title),
    // but exclude tabs and the front page.
    $last = count($trail) - 1;
    if ($trail[$last]['href'] != $item['href'] && !(bool) ($item['type'] & MENU_IS_LOCAL_TASK) && !drupal_is_front_page()) {
      $trail[] = $item;
    }
  }
  return $trail;
}
?>

Comments

Anyone post an example of this using set active trail?

I'm trying to set a trail for a content item that doesn't have a menu item but should appear as if its in a certain sub menu.

I'm having the same problem...

Anyone knows how to solve this problem?
Is it even possible to display a menu for a custom page that doesn't have a menu item linked to it?

This should work

I had success using this snippet to faux the active menu item. I put it in my contemplate but it should also work in hook_nodeapi's view operation

<?php

//Sets the active menu item to foo/bar
$path = drupal_get_normal_path('foo/bar');
$menu_item = menu_get_item($path);
if(
$path && $menu_item) {
 
menu_set_item(NULL, $menu_item);
}
?>

This solution almost worked

This solution almost worked for me. The menu item was being flagged as active, but some of the context blocks on my page did not display. I think it might have to do with the path is changed by the code before the context runs.

The above also works on a views-based page

The above snippet also works on a page generated by the Views module (i.e. a page display type with a path) with some slight modifications:

<?php
  $path
= drupal_get_path_alias(check_plain($_GET['q']));
 
$pieces = explode("/", $path);
 
// Remove the last element of the URI to get the parent
 
array_pop($pieces);
 
$parent_path = implode('/', $pieces); 
 
$menu_item = menu_get_item($parent_path);
  if(
$parent_path && $menu_item) {
   
menu_set_item(NULL, $menu_item);
  }
?>

Place the above code in your view's template file and it should work.

Drupal 7 - Set menu active trail based on node taxonomy term

Lasted few hours but I figured out this solution to set active taxonomy term in menu block. Use function menu_set_active_item in your theme_preprocess_page function in template.php

<?php
theme_preprocess_page
(&$variables) {
   if(isset (
$variables['node']->type) && $variables['node']->type == 'article')  
     
$tid = $node->field_article_taxonomy['und']['0']['tid'];
     
$term = taxonomy_term_load($tid);
     
menu_set_active_item('taxonomy/term/' . $term->tid);
   }
}
?>

Hope it helps

Thank you for your code. I

Thank you for your code. I have spent the last few hours looking for a solution. Your code works perfectly for me.

The only thing is line 3 says:
$tid = $node->field_article_taxonomy['und']['0']['tid'];
it should be
$tid = $variables['node']->field_article_taxonomy['und']['0']['tid'];

Thanks again.

Login or register to post comments