Skip to content

Commit

Permalink
Use destruction events at a later runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
mglaman committed Nov 21, 2023
1 parent 1be6184 commit 7dfa85e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
16 changes: 3 additions & 13 deletions modules/next/next.module
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,21 @@ function next_next_site_preview_alter(array &$preview, array $context) {
*/
function next_entity_insert(EntityInterface $entity) {
$event = EntityActionEvent::createFromEntity($entity, EntityActionEventInterface::INSERT_ACTION);
drupal_register_shutdown_function('_next_dispatch_entity_action_event', $event);
\Drupal::service('next.entity_action_event_dispatcher')->addEvent($event);
}

/**
* Implements hook_entity_update().
*/
function next_entity_update(EntityInterface $entity) {
$event = EntityActionEvent::createFromEntity($entity, EntityActionEventInterface::UPDATE_ACTION);
drupal_register_shutdown_function('_next_dispatch_entity_action_event', $event);
\Drupal::service('next.entity_action_event_dispatcher')->addEvent($event);
}

/**
* Implements hook_entity_predelete().
*/
function next_entity_predelete(EntityInterface $entity) {
$event = EntityActionEvent::createFromEntity($entity, EntityActionEventInterface::DELETE_ACTION);
drupal_register_shutdown_function('_next_dispatch_entity_action_event', $event);
}

/**
* Helper to dispatch an entity action event.
*
* @param \Drupal\next\Event\EntityActionEventInterface $event
* The entity action event.
*/
function _next_dispatch_entity_action_event(EntityActionEventInterface $event) {
\Drupal::service('event_dispatcher')->dispatch($event, EntityEvents::ENTITY_ACTION);
\Drupal::service('next.entity_action_event_dispatcher')->addEvent($event);
}
6 changes: 6 additions & 0 deletions modules/next/next.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ services:
]
tags:
- { name: event_subscriber }

next.entity_action_event_dispatcher:
class: Drupal\next\EventSubscriber\EntityActionEventDispatcher
arguments: ['@event_dispatcher']
tags:
- { name: needs_destruction }
35 changes: 35 additions & 0 deletions modules/next/src/EventSubscriber/EntityActionEventDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Drupal\next\EventSubscriber;

use Drupal\Core\DestructableInterface;
use Drupal\next\Event\EntityActionEvent;
use Drupal\next\Event\EntityEvents;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

final class EntityActionEventDispatcher implements DestructableInterface {

/**
* @var \Drupal\next\Event\EntityActionEvent[]
*/
private array $events = [];

public function __construct(
private EventDispatcherInterface $eventDispatcher
) {
}

public function addEvent(EntityActionEvent $event): void {
$this->events[] = $event;
}

/**
* {@inheritdoc}
*/
public function destruct() {
foreach ($this->events as $event) {
$this->eventDispatcher->dispatch($event, EntityEvents::ENTITY_ACTION);
}
}

}

0 comments on commit 7dfa85e

Please sign in to comment.