You are here

public static function Subscription::postDelete in Commerce Recurring Framework 8

Acts on deleted entities before the delete hook is invoked.

Used after the entities are deleted but 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::postDelete

File

src/Entity/Subscription.php, line 448

Class

Subscription
Defines the subscription entity.

Namespace

Drupal\commerce_recurring\Entity

Code

public static function postDelete(EntityStorageInterface $storage, array $entities) {
  parent::postDelete($storage, $entities);

  // Delete the orders of a deleted subscription. Otherwise they will
  // reference an invalid subscription and result in data integrity issues.
  // Deleting the orders will also remove all order items.
  $orders = [];

  /** @var \Drupal\commerce_recurring\Entity\SubscriptionInterface $entity */
  foreach ($entities as $entity) {
    foreach ($entity
      ->getOrders() as $order) {
      $orders[$order
        ->id()] = $order;
    }
  }

  /** @var \Drupal\commerce_order\OrderStorage $order_storage */
  $order_storage = \Drupal::service('entity_type.manager')
    ->getStorage('commerce_order');
  $order_storage
    ->delete($orders);
}