You are here

function menu_position_activate_rule in Menu Position 7

Same name and namespace in other branches
  1. 7.2 menu_position.module \menu_position_activate_rule()

Activates a specific rule for the given context.

May additionally set the active trail.

Parameters

object $rule: The rule that should be activated.

array $context: A small context variable used by the menu_position module.

bool $set_breadcrumb: Whether to set the active trail / breadcrumb.

1 call to menu_position_activate_rule()
menu_position_evaluate_rules in ./menu_position.module
Evaluate all rules based on a given context.

File

./menu_position.module, line 457
Provides dynamic menu links based on configurable rules.

Code

function menu_position_activate_rule($rule, array $context, $set_breadcrumb) {

  // Retrieve menu item specified in the rule.
  $menu_item = menu_link_load($rule->mlid);

  // Get the current page title.
  $cached_title = drupal_get_title();

  // Sanity check: if the menu link doesn't exist abort processing the rule.
  if (!$menu_item) {
    return FALSE;
  }

  // Reset the menu trail that views may have set.
  $original_router_item = menu_get_item();
  if ($original_router_item['page_callback'] == 'views_page') {
    $preferred =& drupal_static('menu_link_get_preferred');
    unset($preferred[$context['path']]);
  }

  // Set the active path for the rule's menu.
  menu_tree_set_path($rule->menu_name, $menu_item['link_path']);

  // Get the default preferred link and save it so that it can be used in
  // place of the rule's menu link when menu trees are rendered.
  menu_position_set_link($rule->rid, menu_link_get_preferred());

  // Allow the rule's parent menu item to show "expanded" status.
  menu_position_expand_parent_link($rule->plid);

  // Alter the active trail if breadcrumbs still need to be set.
  if ($set_breadcrumb) {

    // Manually set the preferred link for this path so that
    // menu_get_active_trail() returns the proper trail.
    $preferred_links =& drupal_static('menu_link_get_preferred');
    $preferred_links[$_GET['q']][MENU_PREFERRED_LINK] = menu_link_get_preferred($menu_item['link_path']);

    // Reset static trail and breadcrumb caches.
    drupal_static_reset('menu_set_active_trail');
    drupal_static_reset('drupal_set_breadcrumb');

    // Remove the menu position router from the end of the trail.
    $active_trail = menu_set_active_trail();
    array_pop($active_trail);
    menu_set_active_trail($active_trail);
    if (drupal_get_title() !== $cached_title) {
      drupal_set_title($cached_title, PASS_THROUGH);
    }
  }
  return TRUE;
}