Same name and namespace in other branches
  1. 4.7.x includes/menu.inc \menu_rebuild()
  2. 5.x includes/menu.inc \menu_rebuild()
  3. 6.x includes/menu.inc \menu_rebuild()
  4. 7.x includes/menu.inc \menu_rebuild()

Populate the database representation of the menu.

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

Related topics

7 calls to menu_rebuild()
aggregator_edit in modules/aggregator.module
menu_overview in modules/menu.module
Menu callback; present the main menu management page.
system_listing_save in modules/system.module
system_settings_save in modules/system.module
user_admin_perm in modules/user.module
Menu callback: administer permissions.

... See full list

File

includes/menu.inc, line 495
API for the Drupal menu system.

Code

function menu_rebuild() {

  // Clear the page cache, so that changed menus are reflected for anonymous users.
  cache_clear_all();

  // Also clear the menu cache.
  cache_clear_all('menu:', TRUE);
  _menu_build();
  if (module_exist('menu')) {
    $menu = menu_get_menu();
    $new_items = array();
    foreach ($menu['items'] as $mid => $item) {
      if ($mid < 0 && $item['type'] & MENU_MODIFIABLE_BY_ADMIN) {
        $new_mid = db_next_id('{menu}_mid');

        // Check explicitly for mid 1. If the database was improperly prefixed,
        // this would cause a nasty infinite loop.
        // TODO: have automatic prefixing through an installer to prevent this.
        if ($new_mid == 1) {
          $new_mid = db_next_id('{menu}_mid');
        }
        if (isset($new_items[$item['pid']])) {
          $new_pid = $new_items[$item['pid']]['mid'];
        }
        else {
          $new_pid = $item['pid'];
        }

        // Fix parent IDs for menu items already added.
        if ($item['children']) {
          foreach ($item['children'] as $child) {
            if (isset($new_items[$child])) {
              $new_items[$child]['pid'] = $new_mid;
            }
          }
        }
        $new_items[$mid] = array(
          'mid' => $new_mid,
          'pid' => $new_pid,
          'path' => $item['path'],
          'title' => $item['title'],
          'description' => array_key_exists('description', $item) ? $item['description'] : '',
          'weight' => $item['weight'],
          'type' => $item['type'],
        );
      }
    }
    if (count($new_items)) {
      foreach ($new_items as $item) {
        db_query('INSERT INTO {menu} (mid, pid, path, title, description, weight, type) VALUES (%d, %d, \'%s\', \'%s\', \'%s\', %d, %d)', $item['mid'], $item['pid'], $item['path'], $item['title'], $item['description'], $item['weight'], $item['type']);
      }

      // Rebuild the menu to account for the changes.
      _menu_build();
    }
  }
}