menu_link_content.module 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @file
  4. * Allows administrators to create custom menu links.
  5. */
  6. use Drupal\Core\Url;
  7. use Drupal\Core\Entity\EntityInterface;
  8. use Drupal\path_alias\PathAliasInterface;
  9. use Drupal\Core\Routing\RouteMatchInterface;
  10. use Drupal\system\MenuInterface;
  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' => Url::fromRoute('help.page', ['name' => 'menu_ui'])->toString(), ':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. * Implements hook_entity_type_alter().
  32. */
  33. function menu_link_content_entity_type_alter(array &$entity_types) {
  34. // @todo Moderation is disabled for custom menu links until when we have an UI
  35. // for them.
  36. // @see https://www.drupal.org/project/drupal/issues/2350939
  37. $entity_types['menu_link_content']->setHandlerClass('moderation', '');
  38. }
  39. /**
  40. * Implements hook_menu_delete().
  41. */
  42. function menu_link_content_menu_delete(MenuInterface $menu) {
  43. $storage = \Drupal::entityTypeManager()->getStorage('menu_link_content');
  44. $menu_links = $storage->loadByProperties(['menu_name' => $menu->id()]);
  45. $storage->delete($menu_links);
  46. }
  47. /**
  48. * Implements hook_ENTITY_TYPE_insert() for 'path_alias'.
  49. */
  50. function menu_link_content_path_alias_insert(PathAliasInterface $path_alias) {
  51. _menu_link_content_update_path_alias($path_alias->getAlias());
  52. }
  53. /**
  54. * Helper function to update plugin definition using internal scheme.
  55. *
  56. * @param string $path
  57. * The path alias.
  58. */
  59. function _menu_link_content_update_path_alias($path) {
  60. /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
  61. $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  62. /** @var \Drupal\menu_link_content\MenuLinkContentInterface[] $entities */
  63. $entities = \Drupal::entityTypeManager()
  64. ->getStorage('menu_link_content')
  65. ->loadByProperties(['link.uri' => 'internal:' . $path]);
  66. foreach ($entities as $menu_link) {
  67. $menu_link_manager->updateDefinition($menu_link->getPluginId(), $menu_link->getPluginDefinition(), FALSE);
  68. }
  69. }
  70. /**
  71. * Implements hook_ENTITY_TYPE_update() for 'path_alias'.
  72. */
  73. function menu_link_content_path_alias_update(PathAliasInterface $path_alias) {
  74. if ($path_alias->getAlias() != $path_alias->original->getAlias()) {
  75. _menu_link_content_update_path_alias($path_alias->getAlias());
  76. _menu_link_content_update_path_alias($path_alias->original->getAlias());
  77. }
  78. elseif ($path_alias->getPath() != $path_alias->original->getPath()) {
  79. _menu_link_content_update_path_alias($path_alias->getAlias());
  80. }
  81. }
  82. /**
  83. * Implements hook_ENTITY_TYPE_delete() for 'path_alias'.
  84. */
  85. function menu_link_content_path_alias_delete(PathAliasInterface $path_alias) {
  86. _menu_link_content_update_path_alias($path_alias->getAlias());
  87. }
  88. /**
  89. * Implements hook_entity_predelete().
  90. */
  91. function menu_link_content_entity_predelete(EntityInterface $entity) {
  92. /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
  93. $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  94. $entity_type_id = $entity->getEntityTypeId();
  95. foreach ($entity->uriRelationships() as $rel) {
  96. $url = $entity->toUrl($rel);
  97. // Entities can provide uri relationships that are not routed, in this case
  98. // getRouteParameters() will throw an exception.
  99. if (!$url->isRouted()) {
  100. continue;
  101. }
  102. $route_parameters = $url->getRouteParameters();
  103. if (!isset($route_parameters[$entity_type_id])) {
  104. // Do not delete links which do not relate to this exact entity. For
  105. // example, "collection", "add-form", etc.
  106. continue;
  107. }
  108. // Delete all MenuLinkContent links that point to this entity route.
  109. $result = $menu_link_manager->loadLinksByRoute($url->getRouteName(), $route_parameters);
  110. if ($result) {
  111. foreach ($result as $id => $instance) {
  112. if ($instance->isDeletable() && strpos($id, 'menu_link_content:') === 0) {
  113. $instance->deleteLink();
  114. }
  115. }
  116. }
  117. }
  118. }