menu_rebuild

Definition

menu_rebuild()
includes/menu.inc, line 495

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();
  // 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();
    }
  }
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.