You are here

public static function GroupContentMenu::preDelete in Group Content Menu 8

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

src/Entity/GroupContentMenu.php, line 87

Class

GroupContentMenu
Defines the group content menu entity class.

Namespace

Drupal\group_content_menu\Entity

Code

public static function preDelete(EntityStorageInterface $storage, array $entities) {

  /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
  $menu_link_manager = \Drupal::service('plugin.manager.menu.link');

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  foreach ($entities as $entity) {
    $menu_link_manager
      ->deleteLinksInMenu(GroupContentMenuInterface::MENU_PREFIX . $entity
      ->id());
  }

  // Remove any group contents related to this menu before removing the menu.
  if ($entity instanceof ContentEntityInterface) {
    if ($group_contents = GroupContent::loadByEntity($entity)) {

      /** @var \Drupal\group\Entity\GroupContent $group_content */
      foreach ($group_contents as $group_content) {
        $group_content
          ->delete();
      }
    }
  }
}