1.20.x menu.module menu_save($menu)

Save a custom menu.

Parameters

$menu: An array representing a custom menu:

  • menu_name: The unique name of the custom menu (composed of lowercase letters, numbers, and hyphens).
  • title: The human readable menu title.
  • description: The custom menu description.

Modules should always pass a fully populated $menu when saving a custom menu, so other modules are able to output proper status or watchdog messages.

See also

menu_load()

File

modules/menu/menu.module, line 257
Allows administrators to customize the site's menus.

Code

function menu_save($menu) {
  $menu_defaults = array(
    'menu_name' => '',
    'title' => '',
    'description' => '',
  );
  $menu += $menu_defaults;
  $menu = array_intersect_key($menu, $menu_defaults);

  $config = config('menu.menu.' . $menu['menu_name']);
  $status = $config->isNew() ? SAVED_NEW : SAVED_UPDATED;
  $config->setData($menu);
  $config->save();
  menu_cache_clear_all();

  switch ($status) {
    case SAVED_NEW:
      // Make sure the menu is present in the active menus variable so that its
      // items may appear in the menu active trail.
      // @see menu_set_active_menu_names()
      $config = config('system.core');

      $active_menus = $config->get('active_menus_default') ? : array_keys(menu_get_menus());
      if (!in_array($menu['menu_name'], $active_menus)) {
        $active_menus[] = $menu['menu_name'];
        $config->set('active_menus_default', $active_menus)->save();
      }

      module_invoke_all('menu_insert', $menu);
      break;

    case SAVED_UPDATED:
      module_invoke_all('menu_update', $menu);
      break;
  }
}