You are here

function menu_breadcrumb_init in Menu Breadcrumb 7

Same name and namespace in other branches
  1. 6 menu_breadcrumb.module \menu_breadcrumb_init()

Implements hook_init().

Set the active menu according to the current path.

File

./menu_breadcrumb.module, line 316
The main file for the menu_breadcrumb module.

Code

function menu_breadcrumb_init() {
  $is_front = drupal_is_front_page();
  if (variable_get('menu_breadcrumb_determine_menu', 1) && !$is_front) {

    // Find the set of menus containing a link for the current page.
    $menu_item = menu_get_item();
    if ($menu_item === FALSE) {
      return;
    }
    $result = db_query("SELECT mlid, menu_name FROM {menu_links} WHERE link_path = :menu_item", array(
      ':menu_item' => $menu_item['href'],
    ));
    $menu_link_menus = array();
    foreach ($result as $menu_link) {
      $menu_link_menus[$menu_link->menu_name] = TRUE;
    }

    // Choose the highest-priority 'Enabled' menu.
    $match_cache = variable_get('menu_breadcrumb_pattern_matches', array());

    // Enabled menus.
    $menu_list = array_filter(menu_breadcrumb_menu_list());
    foreach (array_keys($menu_list) as $menu_name) {
      $is_pattern = substr($menu_name, 0, 1) == '/' && substr($menu_name, -1, 1) == '/';
      if ($is_pattern) {

        // Look for each of the $menu_link_menus in the pattern match cache.
        foreach (array_keys($menu_link_menus) as $menu_link_menu_name) {
          if (array_key_exists($menu_link_menu_name, $match_cache) && $match_cache[$menu_link_menu_name] == $menu_name) {
            menu_set_active_menu_names($menu_link_menu_name);
            break 2;
          }
        }
      }
      else {
        if (array_key_exists($menu_name, $menu_link_menus)) {
          $active_menus = menu_get_active_menu_names();

          // Add our menu to the front of the active menus list so it takes
          // precedence over all other menus.
          array_unshift($active_menus, $menu_name);
          menu_set_active_menu_names($active_menus);
          break;
        }
      }
    }
  }

  // Generate the breadcrumbs using the active menu.
  $breadcrumb = drupal_get_breadcrumb();

  // If the breadcrumb is just the menu root, try to generate breadcrumb from URL if wanted.
  if (count($breadcrumb) == 1 && variable_get('menu_breadcrumb_no_menu_use_menu_path', 0)) {

    // Try to set breadcrumb based on URL path.
    $breadcrumb = _menu_breadcrumb_by_path($breadcrumb);
  }
  if (variable_get('menu_breadcrumb_append_node_title', 0) == 1) {
    $node_title = filter_xss(menu_get_active_title(), array());
    if (variable_get('menu_breadcrumb_append_node_url', 0) == 1) {
      $breadcrumb[] = $is_front ? l(t('Home'), '<front>') : l(t($node_title), $_GET['q'], array(
        'html' => TRUE,
      ));
    }
    else {
      $breadcrumb[] = $is_front ? t('Home') : t($node_title);
    }
  }
  if (count($breadcrumb) == 1 && variable_get('menu_breadcrumb_hide_on_single_item', 0)) {
    $breadcrumb = array();
  }
  drupal_set_breadcrumb($breadcrumb);
}