menu_link_content.module 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. * Allows administrators to create custom menu links.
  5. */
  6. use Drupal\Core\Entity\EntityInterface;
  7. use Drupal\Core\Routing\RouteMatchInterface;
  8. use Drupal\system\MenuInterface;
  9. /**
  10. * Implements hook_help().
  11. */
  12. function menu_link_content_help($route_name, RouteMatchInterface $route_match) {
  13. switch ($route_name) {
  14. case 'help.page.menu_link_content':
  15. $output = '';
  16. $output .= '<h3>' . t('About') . '</h3>';
  17. $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.');
  18. if (\Drupal::moduleHandler()->moduleExists('menu_ui')) {
  19. $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']);
  20. }
  21. else {
  22. $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']);
  23. }
  24. $output .= '</p>';
  25. return $output;
  26. }
  27. }
  28. /**
  29. * Implements hook_menu_delete().
  30. */
  31. function menu_link_content_menu_delete(MenuInterface $menu) {
  32. $storage = \Drupal::entityManager()->getStorage('menu_link_content');
  33. $menu_links = $storage->loadByProperties(['menu_name' => $menu->id()]);
  34. $storage->delete($menu_links);
  35. }
  36. /**
  37. * Implements hook_path_insert().
  38. */
  39. function menu_link_content_path_insert($path) {
  40. _menu_link_content_update_path_alias($path['alias']);
  41. }
  42. /**
  43. * Helper function to update plugin definition using internal scheme.
  44. *
  45. * @param string $path
  46. * The path alias.
  47. */
  48. function _menu_link_content_update_path_alias($path) {
  49. /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
  50. $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  51. /** @var \Drupal\menu_link_content\MenuLinkContentInterface[] $entities */
  52. $entities = \Drupal::entityManager()
  53. ->getStorage('menu_link_content')
  54. ->loadByProperties(['link.uri' => 'internal:' . $path]);
  55. foreach ($entities as $menu_link) {
  56. $menu_link_manager->updateDefinition($menu_link->getPluginId(), $menu_link->getPluginDefinition(), FALSE);
  57. }
  58. }
  59. /**
  60. * Implements hook_path_update().
  61. */
  62. function menu_link_content_path_update($path) {
  63. if ($path['alias'] != $path['original']['alias']) {
  64. _menu_link_content_update_path_alias($path['alias']);
  65. _menu_link_content_update_path_alias($path['original']['alias']);
  66. }
  67. elseif ($path['source'] != $path['original']['source']) {
  68. _menu_link_content_update_path_alias($path['alias']);
  69. }
  70. }
  71. /**
  72. * Implements hook_path_delete().
  73. */
  74. function menu_link_content_path_delete($path) {
  75. _menu_link_content_update_path_alias($path['alias']);
  76. }
  77. /**
  78. * Implements hook_entity_predelete().
  79. */
  80. function menu_link_content_entity_predelete(EntityInterface $entity) {
  81. /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
  82. $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  83. $entity_type_id = $entity->getEntityTypeId();
  84. foreach ($entity->uriRelationships() as $rel) {
  85. $url = $entity->toUrl($rel);
  86. $route_parameters = $url->getRouteParameters();
  87. if (!isset($route_parameters[$entity_type_id])) {
  88. // Do not delete links which do not relate to this exact entity. For
  89. // example, "collection", "add-form", etc.
  90. continue;
  91. }
  92. // Delete all MenuLinkContent links that point to this entity route.
  93. $result = $menu_link_manager->loadLinksByRoute($url->getRouteName(), $route_parameters);
  94. if ($result) {
  95. foreach ($result as $id => $instance) {
  96. if ($instance->isDeletable() && strpos($id, 'menu_link_content:') === 0) {
  97. $instance->deleteLink();
  98. }
  99. }
  100. }
  101. }
  102. }