Further changes for the Use cases on the live site.
[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\Routing\RouteMatchInterface;
9 use Drupal\system\MenuInterface;
10
11 /**
12  * Implements hook_help().
13  */
14 function menu_link_content_help($route_name, RouteMatchInterface $route_match) {
15   switch ($route_name) {
16     case 'help.page.menu_link_content':
17       $output = '';
18       $output .= '<h3>' . t('About') . '</h3>';
19       $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.');
20       if (\Drupal::moduleHandler()->moduleExists('menu_ui')) {
21         $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']);
22       }
23       else {
24         $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']);
25       }
26       $output .= '</p>';
27       return $output;
28   }
29 }
30
31 /**
32  * Implements hook_menu_delete().
33  */
34 function menu_link_content_menu_delete(MenuInterface $menu) {
35   $storage = \Drupal::entityManager()->getStorage('menu_link_content');
36   $menu_links = $storage->loadByProperties(['menu_name' => $menu->id()]);
37   $storage->delete($menu_links);
38 }
39
40 /**
41  * Implements hook_path_insert().
42  */
43 function menu_link_content_path_insert($path) {
44   _menu_link_content_update_path_alias($path['alias']);
45 }
46
47 /**
48  * Helper function to update plugin definition using internal scheme.
49  *
50  * @param string $path
51  *   The path alias.
52  */
53 function _menu_link_content_update_path_alias($path) {
54   /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
55   $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
56   /** @var \Drupal\menu_link_content\MenuLinkContentInterface[] $entities */
57   $entities = \Drupal::entityManager()
58     ->getStorage('menu_link_content')
59     ->loadByProperties(['link.uri' => 'internal:' . $path]);
60   foreach ($entities as $menu_link) {
61     $menu_link_manager->updateDefinition($menu_link->getPluginId(), $menu_link->getPluginDefinition(), FALSE);
62   }
63 }
64
65 /**
66  * Implements hook_path_update().
67  */
68 function menu_link_content_path_update($path) {
69   if ($path['alias'] != $path['original']['alias']) {
70     _menu_link_content_update_path_alias($path['alias']);
71     _menu_link_content_update_path_alias($path['original']['alias']);
72   }
73   elseif ($path['source'] != $path['original']['source']) {
74     _menu_link_content_update_path_alias($path['alias']);
75   }
76 }
77
78 /**
79  * Implements hook_path_delete().
80  */
81 function menu_link_content_path_delete($path) {
82   _menu_link_content_update_path_alias($path['alias']);
83 }