menu_rebuild

Definition

menu_rebuild()
includes/menu.inc, line 588

Description

Populate the database representation of the menu.

This need only be called at the start of pages that modify the menu.

Related topics

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

Code

<?php
function menu_rebuild() {
  // Clear the page cache, so that changed menus are reflected for anonymous users.
  cache_clear_all('*', 'cache_page', TRUE);
  // Also clear the menu cache.
  cache_clear_all('*', 'cache_menu', TRUE);

  _menu_build();

  if (module_exists('menu')) {
    $menu = menu_get_menu();

    // Fill a queue of new menu items which are modifiable.
    $new_items = array();
    foreach ($menu['items'] as $mid => $item) {
      if ($mid < 0 && ($item['type'] & MENU_MODIFIABLE_BY_ADMIN)) {
        $new_items[$mid] = $item;
      }
    }

    $old_count = -1;
    // Save the new items updating the pids in each iteration
    while (($c = count($new_items)) && ($c != $old_count)) {
      $old_count = count($new_items);
      foreach ($new_items as $mid => $item) {
        // If the item has a valid parent, save it
        if ($item['pid'] >= 0) {
          // The new menu ID gets passed back by reference as $item['mid']
          menu_save_item($item);
          // Fix parent IDs for the children of the menu item just saved
          if ($item['children']) {
            foreach ($item['children'] as $child) {
              if (isset($new_items[$child])) {
                $new_items[$child]['pid'] = $item['mid'];
              }
            }
          }
          // remove the item
          unset($new_items[$mid]);
        }
      }
    }
    // Rebuild the menu to account for the changes.
    _menu_build();
  }

  // Reset the cached $menu in menu_get_item().
  menu_get_item(NULL, NULL, TRUE);

}
?>
 
 

Drupal is a registered trademark of Dries Buytaert.