drupal/core/modules/menu/menu.install

127 lines
3.0 KiB
Plaintext

<?php
/**
* @file
* Install, update and uninstall functions for the menu module.
*/
use Drupal\Component\Uuid\Uuid;
/**
* Implements hook_install().
*/
function menu_install() {
// Add a link for each custom menu.
\Drupal::service('router.builder')->rebuild();
menu_router_rebuild();
$system_link = entity_load_multiple_by_properties('menu_link', array('link_path' => 'admin/structure/menu', 'module' => 'system'));
$system_link = reset($system_link);
$base_link = entity_create('menu_link', array(
'menu_name' => $system_link->menu_name,
'router_path' => 'admin/structure/menu/manage/%',
'module' => 'menu',
));
$menus = entity_load_multiple('menu');
foreach ($menus as $menu) {
$link = $base_link->createDuplicate();
$link->plid = $system_link->id();
$link->link_title = $menu->label();
$link->link_path = 'admin/structure/menu/manage/' . $menu->id();
$query = \Drupal::entityQuery('menu_link')
->condition('link_path', $link->link_path)
->condition('plid', $link->plid);
$result = $query->execute();
if (empty($result)) {
$link->save();
}
}
menu_cache_clear_all();
}
/**
* Implements hook_uninstall().
*/
function menu_uninstall() {
menu_router_rebuild();
}
/**
* Moves menu settings from variables to config.
*
* @ingroup config_upgrade
*/
function menu_update_8000() {
update_variables_to_config('menu.settings', array(
'menu_main_links_source' => 'main_links',
'menu_secondary_links_source' => 'secondary_links',
));
}
/**
* Rename default menu names.
*/
function menu_update_8001() {
// Only the internal identifiers are updated; the labels and descriptions
// might have been customized and do not have to be renamed.
$map = array(
'navigation' => 'tools',
'management' => 'admin',
'user-menu' => 'account',
'main-menu' => 'main',
);
foreach ($map as $old => $new) {
db_update('menu_custom')
->condition('menu_name', $old)
->fields(array('menu_name' => $new))
->execute();
}
}
/**
* Adds the footer menu to custom menus.
*/
function menu_update_8002() {
db_insert('menu_custom')
->fields(array(
'menu_name' => 'footer',
'title' => 'Footer menu',
'description' => 'Use this for linking to site information.',
))
->execute();
}
/**
* Moves menu_override_parent_selector from variables to config.
*
* @ingroup config_upgrade
*/
function menu_update_8003() {
update_variables_to_config('menu.settings', array(
'menu_override_parent_selector' => 'override_parent_selector',
));
}
/**
* Migrate menus into configuration.
*
* @ingroup config_upgrade
*/
function menu_update_8004() {
$uuid = \Drupal::service('uuid');
$result = db_query('SELECT * FROM {menu_custom}');
foreach ($result as $menu) {
// Save the config object.
\Drupal::config('system.menu.' . $menu->menu_name)
->set('id', $menu->menu_name)
->set('uuid', $uuid->generate())
->set('label', $menu->title)
->set('description', $menu->description)
->save();
}
}