You are here

public static function RegistrationType::preDelete in RNG - Events and Registrations 3.x

Same name and namespace in other branches
  1. 8.2 src/Entity/RegistrationType.php \Drupal\rng\Entity\RegistrationType::preDelete()
  2. 8 src/Entity/RegistrationType.php \Drupal\rng\Entity\RegistrationType::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 ConfigEntityBase::preDelete

File

src/Entity/RegistrationType.php, line 63

Class

RegistrationType
Defines the Registration type configuration entity.

Namespace

Drupal\rng\Entity

Code

public static function preDelete(EntityStorageInterface $storage, array $entities) {
  $registration_storage = \Drupal::entityTypeManager()
    ->getStorage('registration');

  /** @var \Drupal\rng\EventManagerInterface $event_manager */
  $event_manager = \Drupal::service('rng.event_manager');

  /** @var \Drupal\rng\Entity\RegistrationTypeInterface $registration_type */
  foreach ($entities as $registration_type) {

    // Remove entity field references in
    // $event->{EventManagerInterface::FIELD_REGISTRATION_TYPE}.
    $event_types = $event_manager
      ->getEventTypes();
    foreach ($event_types as $entity_type => $bundles) {
      $event_storage = \Drupal::entityTypeManager()
        ->getStorage($entity_type);
      foreach ($bundles as $bundle => $event_type) {
        $bundle_key = \Drupal::entityTypeManager()
          ->getDefinition($entity_type)
          ->getKey('bundle');
        $ids = $event_storage
          ->getQuery()
          ->condition($bundle_key, $bundle)
          ->condition(EventManagerInterface::FIELD_REGISTRATION_TYPE, $registration_type
          ->id())
          ->execute();
        foreach ($ids as $id) {
          $event_manager
            ->getMeta($event_storage
            ->load($id))
            ->removeRegistrationType($registration_type
            ->id())
            ->save();
        }
      }
    }

    // Remove registrations.
    $ids = $registration_storage
      ->getQuery()
      ->condition('type', $registration_type
      ->id())
      ->execute();
    $registrations = $registration_storage
      ->loadMultiple($ids);
    $registration_storage
      ->delete($registrations);
  }
  parent::preDelete($storage, $entities);
}