When an entity translation is deleted, add support for also deleting its translated menu item

Created on 31 October 2019, almost 5 years ago
Updated 29 March 2023, over 1 year ago

Title says it all.

The solution I implemented in a custom module is below and fixes the bug.
Is this expected behaviour?

/**
 * Implements hook_entity_translation_delete().
 */
function mmodule_entity_translation_delete(\Drupal\Core\Entity\EntityInterface $translation) {
  $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  $result = $menu_link_manager->loadLinksByRoute('entity.node.canonical', [ 'node' => $translation->id() ]);
  foreach ($result as $id => $menu_link_content) {
    /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $menu_link_content */
    $uuid = $menu_link_content->getDerivativeId();
    /** @var \Drupal\Core\Entity\TranslatableInterface $link */
    $link = \Drupal::service('entity.repository')->loadEntityByUuid('menu_link_content', $uuid);
    if ($link->hasTranslation($translation->language()->getId())) {
      $link->removeTranslation($translation->language()->getId());
      $link->save();
    }
  }
}

Feature request
Status

Active

Version

10.1

Component
Menu system 

Last updated about 21 hours ago

Created by

🇧🇪Belgium weseze

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
  • 🇺🇸United States dww

    Evil English: "it's" == "it is". For possessive, we need "its".

    Meanwhile, I'm not so sure this is a feature request. How can leaving a stale menu item be the right behavior in the original case? This sure seems like a bug to me, but I'm asking in Slack for clarification / discussion.

    Thanks,
    -Derek

  • 🇺🇸United States dww

    Via Slack, @larowlan replied:

    There isn't a 1:1 relationship between menu links and nodes (or any entities)

    I'm not sure we can safely delete a translation of a menu item just because a node had its translation removed for that language

    If we had #2315773: Create a menu link field type/widget/formatter , sure

    That makes sense to me. I suppose the menu items could be really essential for the navigation of the site. If a given translation of a page is now gone, we don't necessarily want to break the menu structure in the other language, but maybe that 1 link now goes back to the original language version of the page it links to. Or whatever. Core can't really know what you expect to happen here, so it's not a bug that we don't do this cleanup automatically for you. But it could be slick and useful to give you a way to clean this up while you're at it...

Production build 0.71.5 2024