You are here

public static function MenuLinkContent::preDelete in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/menu_link_content/src/Entity/MenuLinkContent.php \Drupal\menu_link_content\Entity\MenuLinkContent::preDelete()

Acts on entities before they are deleted and before hooks are invoked.

Used before the entities are deleted and before invoking the delete hook.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

\Drupal\Core\Entity\EntityInterface[] $entities: An array of entities.

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