You are here

function menu_breadcrumb_init in Menu Breadcrumb 6

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

Implementation of hook_init().

Set the active menu according to the current path.

File

./menu_breadcrumb.module, line 301
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();
    $result = db_query("SELECT mlid, menu_name FROM {menu_links} WHERE link_path = '%s'", $menu_item['href']);
    $menu_link_menus = array();
    while ($menu_link = db_fetch_array($result)) {
      $menu_link_menus[$menu_link['menu_name']] = TRUE;
    }

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

    // enabled menus.
    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_name($menu_link_menu_name);
            break 2;
          }
        }
      }
      else {
        if (array_key_exists($menu_name, $menu_link_menus)) {
          menu_set_active_menu_name($menu_name);
          break;
        }
      }
    }
  }

  // Generate the breadcrumbs using the active menu.
  $breadcrumb = drupal_get_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($node_title, $_GET['q'], array(
        'html' => TRUE,
      ));
    }
    else {
      $breadcrumb[] = $is_front ? t('Home') : $node_title;
    }
  }
  if (count($breadcrumb) == 1 && variable_get('menu_breadcrumb_hide_on_single_item', 0)) {
    $breadcrumb = array();
  }
  drupal_set_breadcrumb($breadcrumb);
}