function MenuLinkContent::preDelete

Same name in other branches
  1. 9 core/modules/menu_link_content/src/Entity/MenuLinkContent.php \Drupal\menu_link_content\Entity\MenuLinkContent::preDelete()
  2. 10 core/modules/menu_link_content/src/Entity/MenuLinkContent.php \Drupal\menu_link_content\Entity\MenuLinkContent::preDelete()
  3. 11.x core/modules/menu_link_content/src/Entity/MenuLinkContent.php \Drupal\menu_link_content\Entity\MenuLinkContent::preDelete()

Overrides EntityBase::preDelete

File

core/modules/menu_link_content/src/Entity/MenuLinkContent.php, line 244

Class

MenuLinkContent
Defines the menu link content entity class.

Namespace

Drupal\menu_link_content\Entity

Code

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');
    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->getParentId();
        $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();
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.