You are here

function menu_trail_by_path_page_delivery_callback_alter in Menu Trail By Path 7.2

Same name and namespace in other branches
  1. 7.3 menu_trail_by_path.module \menu_trail_by_path_page_delivery_callback_alter()

Implements hook_page_delivery_callback_alter().

This is the only hook that occurs after the page callback, but before hook_page_build (when blocks are added). We're using this hook for its timing, not its data.

File

./menu_trail_by_path.module, line 23
Expand menu items and set active-trail according to current path.

Code

function menu_trail_by_path_page_delivery_callback_alter() {
  global $language;
  $parent_candidates = _menu_trail_by_path_get_parent_candidates(drupal_get_path_alias());

  // Don't even bother if current page is root.
  if (empty($parent_candidates)) {
    return;
  }

  // Find link items matching the parent candidates in all menus.
  $matched_menus = array();
  $matched_link_titles = array();
  $results = db_select('menu_links', 'ml')
    ->fields('ml', array(
    'menu_name',
    'mlid',
    'link_path',
    'link_title',
    'depth',
    'weight',
  ))
    ->condition('link_path', $parent_candidates, 'IN')
    ->condition('menu_name', 'management', '!=')
    ->condition('hidden', 0)
    ->execute();
  foreach ($results as $record) {

    // If there is more than one matched link in a menu,
    // use the deepest, heaviest.
    if (!isset($matched_menus[$record->menu_name]) || $record->depth > $matched_menus[$record->menu_name]['depth'] || $record->depth == $matched_menus[$record->menu_name]['depth'] && $record->weight > $matched_menus[$record->menu_name]['weight']) {
      $matched_menus[$record->menu_name]['link_path'] = $record->link_path;
      $matched_menus[$record->menu_name]['depth'] = $record->depth;
      $matched_menus[$record->menu_name]['weight'] = $record->weight;
    }

    // Get the Link Title if it can be found in a menu item.
    if ($record->link_title && !isset($matched_link_titles[$record->link_path])) {
      $matched_link_titles[$record->link_path] = $record->link_title;
      if (module_exists('i18n_menu')) {
        $matched_link_titles[$record->link_path] = _i18n_menu_link_title((array) $record, $language->language);
      }
    }
  }

  // Set the active-trail for each menu containing one of the candidates.
  foreach ($matched_menus as $menu_name => $menu_link) {
    menu_tree_set_path($menu_name, $menu_link['link_path']);
  }

  // Set the breadcrumbs according to path URL if it is enabled in the UI.
  if (variable_get('menu_trail_by_path_breadcrumb_handling', TRUE)) {

    // First breadcrumb is always Home.
    $breadcrumbs[] = l(t('Home'), '<front>');

    // Remove current page from breadcrumb.
    array_pop($parent_candidates);
    foreach ($parent_candidates as $link_path) {

      // If the page title is found on a menu item, use it.
      if (isset($matched_link_titles[$link_path])) {
        $breadcrumbs[] = l($matched_link_titles[$link_path], $link_path);
      }
      elseif ($menu_item = menu_get_item($link_path)) {
        if (!empty($menu_item['title'])) {
          $breadcrumbs[] = l($menu_item['title'], $link_path);
        }
      }
    }
    drupal_set_breadcrumb($breadcrumbs);
  }
}