Skip to content
Snippets Groups Projects

Issue #2759275: Cannot delete a config menu link

@@ -16,7 +16,8 @@ use Drupal\menu_link_config\MenuLinkConfigInterface;
* handlers = {
* "access" = "\Drupal\Core\Entity\EntityAccessControlHandler",
* "form" = {
* "default" = "\Drupal\menu_link_config\Plugin\Menu\Form\MenuLinkConfigForm"
* "default" =
* "\Drupal\menu_link_config\Plugin\Menu\Form\MenuLinkConfigForm"
* }
* },
* admin_permission = "administer menu link config",
@@ -170,13 +171,44 @@ class MenuLinkConfig extends ConfigEntityBase implements MenuLinkConfigInterface
/** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
// The menu link can just be updated if there is already an menu link entry
// The menu link can just be updated if there is already a menu link entry
// on both entity and menu link plugin level.
if ($update && $menu_link_manager->getDefinition($this->getPluginId())) {
$menu_link_manager->updateDefinition($this->getPluginId(), $this->getPluginDefinition(), FALSE);
$definition = $this->getPluginDefinition();
// Even when $update is FALSE, for top level links it is possible the link
// already is in the storage because of the getPluginDefinition() call
// above, see https://www.drupal.org/node/2605684#comment-10515450 for the
// call chain. Because of this the $update flag is ignored and only the
// existence of the definition (equals to being in the tree storage) is
// checked.
if ($menu_link_manager->getDefinition($this->getPluginId(), FALSE)) {
$menu_link_manager->updateDefinition($this->getPluginId(), $definition, FALSE);
}
else {
$menu_link_manager->addDefinition($this->getPluginId(), $this->getPluginDefinition());
$menu_link_manager->addDefinition($this->getPluginId(), $definition);
}
}
/**
* {@inheritdoc}
*/
public static function preDelete(EntityStorageInterface $storage, array $entities) {
parent::preDelete($storage, $entities);
/** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
/** @var \Drupal\menu_link_config\Entity\MenuLinkConfig $menu_link */
foreach ($entities as $menu_link) {
/** @var \Drupal\menu_link_content\Entity\MenuLinkContent $menu_link */
$menu_link_manager->removeDefinition($menu_link->getPluginId(), FALSE);
// Children get re-attached to the menu link's parent.
$parent_plugin_id = $menu_link->getParent();
$children = $storage->loadByProperties(['parent' => $menu_link->getPluginId()]);
foreach ($children as $child) {
/** @var \Drupal\menu_link_content\Entity\MenuLinkContent $child */
$child->set('parent', $parent_plugin_id)->save();
}
}
}
Loading