menu_primary_links

Definition

menu_primary_links($start_level = 1, $pid = 0)
drupal/includes/menu.inc, line 832

Description

Returns an array containing the primary links. Can optionally descend from the root of the Primary links menu towards the current node for a specified number of levels and return that submenu. Used to generate a primary/secondary menu from different levels of one menu.

Parameters

$start_level This optional parameter can be used to retrieve a context-sensitive array of links at $start_level levels deep into the Primary links menu. The default is to return the top-level links.

$pid The parent menu ID from which to search for children. Defaults to the menu_primary_menu setting.

Return value

A nested array of links and their properties. The keys of the array contain some extra encoded information about the results. The format of the key is {level}-{num}{-active}. level is the depth within the menu tree of this list. num is the number within this array, used only to make the key unique. -active is appended if this element is in the active trail.

Related topics

Namesort iconDescription
Menu systemDefine the navigation menus, and route page requests to code based on URLs.

Code

function menu_primary_links($start_level = 1, $pid = 0) {
  if (!module_exists('menu')) {
    return NULL;
  }
  if (!$pid) {
    $pid = variable_get('menu_primary_menu', 0);
  }
  if (!$pid) {
    return NULL;
  }

  if ($start_level < 1) {
    $start_level = 1;
  }

  if ($start_level > 1) {
    $trail = _menu_get_active_trail_in_submenu($pid);
    if (!$trail) {
      return NULL;
    }
    else {
      $pid = $trail[$start_level - 1];
    }
  }

  $menu = menu_get_menu();
  $links = array();
  if ($pid && is_array($menu['visible'][$pid]) && isset($menu['visible'][$pid]['children'])) {
    $count = 1;
    foreach ($menu['visible'][$pid]['children'] as $cid) {
      $index = "menu-$start_level-$count-$pid";
      if (menu_in_active_trail_in_submenu($cid, $pid)) {
        $index .= "-active";
      }
      $links[$index] = menu_item_link($cid, FALSE);
      $count++;
    }
  }

  // Special case - provide link to admin/build/menu if primary links is empty.
  if (empty($links) && $start_level == 1 && $pid == variable_get('menu_primary_menu', 0) && user_access('administer menu')) {
    $links['1-1'] = array(
      'title' => t('Edit primary links'),
      'href' => 'admin/build/menu'
    );
  }

  return $links;
}