a5541cda26199e559a2975630a500f92c6f70fbb
[yaffs-website] / web / core / modules / menu_link_content / menu_link_content.module
1 <?php
2
3 /**
4  * @file
5  * Allows administrators to create custom menu links.
6  */
7
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Routing\RouteMatchInterface;
10 use Drupal\system\MenuInterface;
11
12 /**
13  * Implements hook_help().
14  */
15 function menu_link_content_help($route_name, RouteMatchInterface $route_match) {
16   switch ($route_name) {
17     case 'help.page.menu_link_content':
18       $output = '';
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']);
23       }
24       else {
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']);
26       }
27       $output .= '</p>';
28       return $output;
29   }
30 }
31
32 /**
33  * Implements hook_menu_delete().
34  */
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);
39 }
40
41 /**
42  * Implements hook_path_insert().
43  */
44 function menu_link_content_path_insert($path) {
45   _menu_link_content_update_path_alias($path['alias']);
46 }
47
48 /**
49  * Helper function to update plugin definition using internal scheme.
50  *
51  * @param string $path
52  *   The path alias.
53  */
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);
63   }
64 }
65
66 /**
67  * Implements hook_path_update().
68  */
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']);
73   }
74   elseif ($path['source'] != $path['original']['source']) {
75     _menu_link_content_update_path_alias($path['alias']);
76   }
77 }
78
79 /**
80  * Implements hook_path_delete().
81  */
82 function menu_link_content_path_delete($path) {
83   _menu_link_content_update_path_alias($path['alias']);
84 }
85
86 /**
87  * Implements hook_entity_predelete().
88  */
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   $entity_type_id = $entity->getEntityTypeId();
93   foreach ($entity->uriRelationships() as $rel) {
94     $url = $entity->toUrl($rel);
95     $route_parameters = $url->getRouteParameters();
96     if (!isset($route_parameters[$entity_type_id])) {
97       // Do not delete links which do not relate to this exact entity. For
98       // example, "collection", "add-form", etc.
99       continue;
100     }
101     // Delete all MenuLinkContent links that point to this entity route.
102     $result = $menu_link_manager->loadLinksByRoute($url->getRouteName(), $route_parameters);
103
104     if ($result) {
105       foreach ($result as $id => $instance) {
106         if ($instance->isDeletable() && strpos($id, 'menu_link_content:') === 0) {
107           $instance->deleteLink();
108         }
109       }
110     }
111   }
112 }