_menu_build
- Versions
- 4.6 – 5
_menu_build()
Build the menu by querying both modules and the database.
Code
includes/menu.inc, line 746
<?php
function _menu_build() {
global $_menu;
global $user;
// Start from a clean slate.
$_menu = array();
$_menu['path index'] = array();
// Set up items array, including default "Navigation" menu.
$_menu['items'] = array(
0 => array('path' => '', 'title' => '', 'type' => MENU_IS_ROOT),
1 => array('pid' => 0, 'path' => '', 'title' => t('Navigation'), 'weight' => -50, 'access' => TRUE, 'type' => MENU_IS_ROOT | MENU_VISIBLE_IN_TREE)
);
// Build a sequential list of all menu items.
$menu_item_list = module_invoke_all('menu', TRUE);
// Menu items not in the DB get temporary negative IDs.
$temp_mid = -1;
foreach ($menu_item_list as $item) {
if (!array_key_exists('path', $item)) {
$item['path'] = '';
}
if (!array_key_exists('type', $item)) {
$item['type'] = MENU_NORMAL_ITEM;
}
if (!array_key_exists('weight', $item)) {
$item['weight'] = 0;
}
$mid = $temp_mid;
if (array_key_exists($item['path'], $_menu['path index'])) {
// Newer menu items overwrite older ones.
unset($_menu['items'][$_menu['path index'][$item['path']]]);
}
$_menu['items'][$mid] = $item;
$_menu['path index'][$item['path']] = $mid;
$temp_mid--;
}
// Now fetch items from the DB, reassigning menu IDs as needed.
if (module_exist('menu')) {
$result = db_query('SELECT * FROM {menu} ORDER BY mid ASC');
while ($item = db_fetch_object($result)) {
// Handle URL aliases if entered in menu administration.
$item->path = drupal_get_normal_path($item->path);
if (array_key_exists($item->path, $_menu['path index'])) {
// The path is already declared.
$old_mid = $_menu['path index'][$item->path];
if ($old_mid < 0) {
// It had a temporary ID, so use a permanent one.
$_menu['items'][$item->mid] = $_menu['items'][$old_mid];
unset($_menu['items'][$old_mid]);
$_menu['path index'][$item->path] = $item->mid;
}
else {
// It has a permanent ID. Only replace with non-custom menu items.
if ($item->type & MENU_CREATED_BY_ADMIN) {
$_menu['items'][$item->mid] = array('path' => $item->path, 'callback' => '');
}
else {
// Leave the old item around as a shortcut to this one.
$_menu['items'][$item->mid] = $_menu['items'][$old_mid];
$_menu['path index'][$item->path] = $item->mid;
}
}
}
else {
// The path was not declared, so this is a custom item or an orphaned one.
if ($item->type & MENU_CREATED_BY_ADMIN) {
$_menu['items'][$item->mid] = array('path' => $item->path, 'callback' => '');
if (!empty($item->path)) {
$_menu['path index'][$item->path] = $item->mid;
}
}
}
// If the administrator has changed the item, reflect the change.
if ($item->type & MENU_MODIFIED_BY_ADMIN) {
$_menu['items'][$item->mid]['title'] = $item->title;
$_menu['items'][$item->mid]['description'] = $item->description;
$_menu['items'][$item->mid]['pid'] = $item->pid;
$_menu['items'][$item->mid]['weight'] = $item->weight;
$_menu['items'][$item->mid]['type'] = $item->type;
}
}
}
// Associate parent and child menu items.
_menu_find_parents($_menu['items']);
// Prepare to display trees to the user as required.
_menu_build_visible_tree();
}
?>Login or register to post comments 