Skip navigation
Help

menu_set_active_trail

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

Sets or gets the active trail (path to menu tree 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

1 call to menu_set_active_trail()

File

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

Code

function menu_set_active_trail($new_trail = NULL) {
  $trail = &drupal_static(__FUNCTION__);

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

    // Try to retrieve a menu link corresponding to the current path. If more
    // than one exists, the link from the most preferred menu is returned.
    $preferred_link = menu_link_get_preferred();
    $current_item = menu_get_item();

    // There is a link for the current path.
    if ($preferred_link) {
      // Pass TRUE for $only_active_trail to make menu_tree_page_data() build
      // a stripped down menu tree containing the active trail only, in case
      // the given menu has not been built in this request yet.
      $tree = menu_tree_page_data($preferred_link['menu_name'], NULL, TRUE);
      list($key, $curr) = each($tree);
    }
    // There is no link for the current path.
    else {
      $preferred_link = $current_item;
      $curr = FALSE;
    }

    while ($curr) {
      $link = $curr['link'];
      if ($link['in_active_trail']) {
        // Add the link to the trail, unless it links to its parent.
        if (!($link['type'] & MENU_LINKS_TO_PARENT)) {
          // The menu tree for the active trail may contain additional links
          // that have not been translated yet, since they contain dynamic
          // argument placeholders (%). Such links are not contained in regular
          // menu trees, and have only been loaded for the additional
          // translation that happens here, so as to be able to display them in
          // the breadcumb for the current page.
          // @see _menu_tree_check_access()
          // @see _menu_link_translate()
          if (strpos($link['href'], '%') !== FALSE) {
            _menu_link_translate($link, TRUE);
          }
          if ($link['access']) {
            $trail[] = $link;
          }
        }
        $tree = $curr['below'] ? $curr['below'] : array();
      }
      list($key, $curr) = each($tree);
    }
    // Make sure the current page is in the trail to build the page title, by
    // appending either the preferred link or the menu router item for the
    // current page. Exclude it if we are on the front page.
    $last = end($trail);
    if ($last['href'] != $preferred_link['href'] && !drupal_is_front_page()) {
      $trail[] = $preferred_link;
    }
  }
  return $trail;
}