You are here

public static function MailingList::postDelete in Mailing List 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 ConfigEntityBundleBase::postDelete

File

src/Entity/MailingList.php, line 272

Class

MailingList
Defines the mailing list configuration entity.

Namespace

Drupal\mailing_list\Entity

Code

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

  // Remove permissions of the mailing list.
  $roles = \Drupal::entityTypeManager()
    ->getStorage('user_role')
    ->loadMultiple();
  foreach ($entities as $entity) {
    $list_id = $entity
      ->id();
    foreach ($roles as $role) {
      $save_role = FALSE;

      /* @var \Drupal\user\RoleInterface $role */
      foreach ([
        "subscribe to {$list_id} mailing list",
        "access inactive {$list_id} mailing list subscriptions",
        "view any {$list_id} mailing list subscriptions",
        "update any {$list_id} mailing list subscriptions",
        "delete any {$list_id} mailing list subscriptions",
      ] as $permission) {
        if ($role
          ->hasPermission($permission)) {
          $save_role = TRUE;
          $role
            ->revokePermission($permission);
        }
      }
      if ($save_role) {
        $role
          ->save();
      }
    }

    // Remove subscription blocks.
    foreach (\Drupal::entityTypeManager()
      ->getStorage('block')
      ->loadMultiple(\Drupal::entityQuery('block')
      ->condition('plugin', 'mailing_list_subscription_block')
      ->condition('settings.list', $entity
      ->id())
      ->execute()) as $block) {
      $block
        ->delete();
    }
  }

  // Clear the cache to reflect the removal.
  $storage
    ->resetCache(array_keys($entities));
}