You are here

function _hansel_activate_menu in Hansel breadcrumbs 7

Same name and namespace in other branches
  1. 8 hansel.module \_hansel_activate_menu()

Set the active menu item to the item corresponding the breadcrumbs.

1 call to _hansel_activate_menu()
hansel_init in ./hansel.module
Implements hook_init().

File

./hansel.module, line 917
Hansel module

Code

function _hansel_activate_menu() {
  $path = hansel_get_breadcrumbs(FALSE, TRUE, TRUE, array(
    'menu',
  ));
  if (is_array($path)) {
    $path = $path['breadcrumb'];
  }
  if (variable_get('hansel_set_menu_item_skip_first', TRUE)) {
    array_shift($path);
  }

  // Build a list of menu names. We will only look for items in one of these menu's.
  $menus = menu_get_menus();
  if (isset($menus['devel'])) {

    // Do not use the development menu.
    unset($menus['devel']);
  }
  $menus = array_keys($menus);

  // Let other modules alter the menu list.
  drupal_alter('hansel_menus', $menus);
  if (!$menus) {

    // No menu's to search for items.
    return;
  }
  $link_path = FALSE;
  $plid = 0;
  for ($i = 0; $i < count($path); ++$i) {
    $query = db_select('menu_links', 'ml')
      ->fields('ml', array(
      'menu_name',
      'mlid',
      'link_path',
    ))
      ->condition('link_title', $path[$i])
      ->condition('plid', $plid)
      ->condition('menu_name', $menus, 'in')
      ->range(0, 1);
    if (module_exists('i18n_menu')) {
      global $language;
      $or = db_or()
        ->condition('ml.language', LANGUAGE_NONE)
        ->condition('ml.language', $language->language);
      $query
        ->condition($or);
    }
    $link = $query
      ->execute()
      ->fetchObject();
    if ($link) {
      $plid = $link->mlid;
      $link_path = $link->link_path;
      $menu_name = $link->menu_name;
    }
  }
  if ($link_path && $link_path !== '<front>') {
    $item = menu_get_item();
    $item['href'] = $link_path;
    if (!empty($item['access'])) {
      menu_set_item(NULL, $item);
      menu_tree_set_path($menu_name, $link_path);
    }
    $menus = menu_set_active_menu_names();
    if (!in_array($menu_name, $menus)) {
      $menus[] = $menu_name;
      menu_set_active_menu_names($menus);
    }
  }
}