You are here

function menu_block_get_current_page_menu in Menu Block 7.3

Same name and namespace in other branches
  1. 7.2 menu_block.module \menu_block_get_current_page_menu()

Returns the current page's menu.

Return value

string|bool The current page's menu, or FALSE if no menu applied.

1 call to menu_block_get_current_page_menu()
menu_tree_build in ./menu_block.module
Build a menu tree based on the provided configuration.

File

./menu_block.module, line 424
Provides configurable blocks of menu items.

Code

function menu_block_get_current_page_menu() {

  // Retrieve the list of available menus.
  $menu_order = variable_get('menu_block_menu_order', array(
    'main-menu' => '',
    'user-menu' => '',
  ));

  // Check for regular expressions as menu keys.
  $patterns = array();
  foreach (array_keys($menu_order) as $pattern) {
    if ($pattern[0] == '/') {
      $patterns[$pattern] = NULL;
    }
  }

  // Extract the "current" path from the request, or from the active menu
  // trail if applicable.
  $link_path = $_GET['q'] ? $_GET['q'] : '<front>';
  $trail = menu_get_active_trail();
  $last_item = end($trail);
  if (!empty($last_item['link_path'])) {
    $link_path = $last_item['link_path'];
  }

  // Retrieve all the menus containing a link to the current page.
  $result = db_query("SELECT menu_name FROM {menu_links} WHERE link_path = :link_path", array(
    ':link_path' => $link_path,
  ));
  foreach ($result as $item) {

    // Check if the menu is in the list of available menus.
    if (isset($menu_order[$item->menu_name])) {

      // Mark the menu.
      $menu_order[$item->menu_name] = MENU_TREE__CURRENT_PAGE_MENU;
    }
    else {

      // Check if the menu matches one of the available patterns.
      foreach (array_keys($patterns) as $pattern) {
        if (preg_match($pattern, $item->menu_name)) {

          // Mark the menu.
          $menu_order[$pattern] = MENU_TREE__CURRENT_PAGE_MENU;

          // Store the actual menu name.
          $patterns[$pattern] = $item->menu_name;
        }
      }
    }
  }

  // Find the first marked menu.
  $menu_name = array_search(MENU_TREE__CURRENT_PAGE_MENU, $menu_order);

  // If a pattern was matched, use the actual menu name instead of the pattern.
  if (!empty($patterns[$menu_name])) {
    $menu_name = $patterns[$menu_name];
  }
  return $menu_name;
}