5 * Allows administrators to create custom menu links.
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Routing\RouteMatchInterface;
10 use Drupal\system\MenuInterface;
13 * Implements hook_help().
15 function menu_link_content_help($route_name, RouteMatchInterface $route_match) {
16 switch ($route_name) {
17 case 'help.page.menu_link_content':
19 $output .= '<h3>' . t('About') . '</h3>';
20 $output .= '<p>' . t('The Custom Menu Links module allows users to create menu links. These links can be translated if multiple languages are used for the site.');
21 if (\Drupal::moduleHandler()->moduleExists('menu_ui')) {
22 $output .= ' ' . t('It is required by the Menu UI module, which provides an interface for managing menus and menu links. For more information, see the <a href=":menu-help">Menu UI module help page</a> and the <a href=":drupal-org-help">online documentation for the Custom Menu Links module</a>.', [':menu-help' => \Drupal::url('help.page', ['name' => 'menu_ui']), ':drupal-org-help' => 'https://www.drupal.org/documentation/modules/menu_link']);
25 $output .= ' ' . t('For more information, see the <a href=":drupal-org-help">online documentation for the Custom Menu Links module</a>. If you enable the Menu UI module, it provides an interface for managing menus and menu links.', [':drupal-org-help' => 'https://www.drupal.org/documentation/modules/menu_link']);
33 * Implements hook_menu_delete().
35 function menu_link_content_menu_delete(MenuInterface $menu) {
36 $storage = \Drupal::entityManager()->getStorage('menu_link_content');
37 $menu_links = $storage->loadByProperties(['menu_name' => $menu->id()]);
38 $storage->delete($menu_links);
42 * Implements hook_path_insert().
44 function menu_link_content_path_insert($path) {
45 _menu_link_content_update_path_alias($path['alias']);
49 * Helper function to update plugin definition using internal scheme.
54 function _menu_link_content_update_path_alias($path) {
55 /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
56 $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
57 /** @var \Drupal\menu_link_content\MenuLinkContentInterface[] $entities */
58 $entities = \Drupal::entityManager()
59 ->getStorage('menu_link_content')
60 ->loadByProperties(['link.uri' => 'internal:' . $path]);
61 foreach ($entities as $menu_link) {
62 $menu_link_manager->updateDefinition($menu_link->getPluginId(), $menu_link->getPluginDefinition(), FALSE);
67 * Implements hook_path_update().
69 function menu_link_content_path_update($path) {
70 if ($path['alias'] != $path['original']['alias']) {
71 _menu_link_content_update_path_alias($path['alias']);
72 _menu_link_content_update_path_alias($path['original']['alias']);
74 elseif ($path['source'] != $path['original']['source']) {
75 _menu_link_content_update_path_alias($path['alias']);
80 * Implements hook_path_delete().
82 function menu_link_content_path_delete($path) {
83 _menu_link_content_update_path_alias($path['alias']);
87 * Implements hook_entity_predelete().
89 function menu_link_content_entity_predelete(EntityInterface $entity) {
90 /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
91 $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
92 foreach ($entity->uriRelationships() as $rel) {
93 $url = $entity->toUrl($rel);
94 // Delete all MenuLinkContent links that point to this entity route.
95 $result = $menu_link_manager->loadLinksByRoute($url->getRouteName(), $url->getRouteParameters());
98 foreach ($result as $id => $instance) {
99 if ($instance->isDeletable() && strpos($id, 'menu_link_content:') === 0) {
100 $instance->deleteLink();