public static function Newsletter::postDelete in Simplenews 3.x
Same name and namespace in other branches
- 8.2 src/Entity/Newsletter.php \Drupal\simplenews\Entity\Newsletter::postDelete()
- 8 src/Entity/Newsletter.php \Drupal\simplenews\Entity\Newsletter::postDelete()
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/ Newsletter.php, line 172
Class
- Newsletter
- Defines the simplenews newsletter entity.
Namespace
Drupal\simplenews\EntityCode
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
/** @var \Drupal\simplenews\Subscription\SubscriptionStorageInterface $subscription_storage */
$subscription_storage = \Drupal::entityTypeManager()
->getStorage('simplenews_subscriber');
foreach ($entities as $newsletter) {
$subscription_storage
->deleteSubscriptions([
'subscriptions_target_id' => $newsletter
->id(),
]);
\Drupal::messenger()
->addMessage(t('All subscriptions to newsletter %newsletter have been deleted.', [
'%newsletter' => $newsletter
->label(),
]));
}
if (\Drupal::moduleHandler()
->moduleExists('block')) {
// Make sure there are no active blocks for these newsletters.
$ids = \Drupal::entityQuery('block')
->condition('plugin', 'simplenews_subscription_block')
->condition('settings.newsletters.*', array_keys($entities), 'IN')
->execute();
if ($ids) {
$blocks = Block::loadMultiple($ids);
foreach ($blocks as $block) {
$settings = $block
->get('settings');
foreach ($entities as $newsletter) {
if (in_array($newsletter
->id(), $settings['newsletters'])) {
unset($settings['newsletters'][array_search($newsletter
->id(), $settings['newsletters'])]);
}
}
// If there are no enabled newsletters left, delete the block.
if (empty($settings['newsletters'])) {
$block
->delete();
}
else {
// otherwise, update the settings and save.
$block
->set('settings', $settings);
$block
->save();
}
}
}
}
}