X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fmenu_link_content%2Fmenu_link_content.module;fp=web%2Fcore%2Fmodules%2Fmenu_link_content%2Fmenu_link_content.module;h=f94a139d4623a2ab6f16c4f2bededfccfd28a96c;hp=0e02ba948b508f7109dfaf3cd147737faf472edf;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/modules/menu_link_content/menu_link_content.module b/web/core/modules/menu_link_content/menu_link_content.module index 0e02ba948..f94a139d4 100644 --- a/web/core/modules/menu_link_content/menu_link_content.module +++ b/web/core/modules/menu_link_content/menu_link_content.module @@ -5,6 +5,7 @@ * Allows administrators to create custom menu links. */ +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\system\MenuInterface; @@ -81,3 +82,24 @@ function menu_link_content_path_update($path) { function menu_link_content_path_delete($path) { _menu_link_content_update_path_alias($path['alias']); } + +/** + * Implements hook_entity_predelete(). + */ +function menu_link_content_entity_predelete(EntityInterface $entity) { + /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */ + $menu_link_manager = \Drupal::service('plugin.manager.menu.link'); + foreach ($entity->uriRelationships() as $rel) { + $url = $entity->toUrl($rel); + // Delete all MenuLinkContent links that point to this entity route. + $result = $menu_link_manager->loadLinksByRoute($url->getRouteName(), $url->getRouteParameters()); + + if ($result) { + foreach ($result as $id => $instance) { + if ($instance->isDeletable() && strpos($id, 'menu_link_content:') === 0) { + $instance->deleteLink(); + } + } + } + } +}