You are here

function context_ui_menu_set_location in Context 5

Alters the menu to reflect any active contexts. Called from context_ui_set().

1 call to context_ui_menu_set_location()
context_ui_context_getter in context_ui/context_ui.module
Implementation of hook_context_getter().

File

context_ui/context_ui.module, line 472

Code

function context_ui_menu_set_location($context) {
  if ($context->cid) {
    $result = db_query("\n      SELECT ci.id\n      FROM {context_ui_item} ci\n      WHERE ci.type = '%s' AND ci.cid = %d", 'menu', $context->cid);
    if ($path = db_result($result)) {

      // Load global menu -- unfortunately we may have to modify it manually
      global $_menu;

      // Get menu item for active context
      $context_menu = menu_get_item(null, $path);
      $items[] = $context_menu;

      // Grab the menu tree from active context item to the root
      $mid = $context_menu['pid'];
      while ($mid && ($item = menu_get_item($mid))) {
        $items[] = $item;
        $mid = $item['pid'];
      }
      $items = array_reverse($items);

      // Graft the current active page on if it is different
      $active_mid = menu_get_active_item();
      if ($active_mid != $_menu['path index'][$path] && !$items[$active_mid]) {
        $active_menu = menu_get_item($active_mid);

        // Due to a bug/feature? of menu_set_location() menu leaves that
        // are part of the 'visible' portion of the menu tree short-circuit
        // any further menu grafting. We check here, and make sure the leaf
        // item is not a visible one.
        if (isset($_menu['visible'][$active_mid])) {
          unset($_menu['visible'][$active_mid]);
        }

        // If the active menu item is a local task we need to make sure
        // to include it's parent.
        if ($active_menu['type'] & MENU_IS_LOCAL_TASK) {
          $parent_menu = menu_get_item($active_menu['pid']);
          $items[] = $parent_menu;

          // Allow for two levels of local tasks.
          if ($parent_menu['type'] & MENU_IS_LOCAL_TASK) {
            $items[] = menu_get_item($parent_menu['pid']);
          }
        }
        if ($active_menu['type'] & MENU_IS_LOCAL_TASK & MENU_LINKS_TO_PARENT) {
          $real_q = end($items);
          $real_q = $real_q['path'];
        }
        $items[] = $active_menu;
      }

      // Push the new location path through
      menu_set_location($items);

      // Repair query string
      if ($real_q) {
        $_GET['q'] = $real_q;
      }
    }
  }
}