You are here

public function RngRequestSubscriber::onKernelTerminate in RNG - Events and Registrations 3.x

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/RngRequestSubscriber.php \Drupal\rng\EventSubscriber\RngRequestSubscriber::onKernelTerminate()
  2. 8 src/EventSubscriber/RngRequestSubscriber.php \Drupal\rng\EventSubscriber\RngRequestSubscriber::onKernelTerminate()

Run RNG rules for entity operations which occurred during this request.

Parameters

\Symfony\Component\HttpKernel\Event\PostResponseEvent $event: The event to process.

File

src/EventSubscriber/RngRequestSubscriber.php, line 60

Class

RngRequestSubscriber
Runs tasks for RNG related to the request.

Namespace

Drupal\rng\EventSubscriber

Code

public function onKernelTerminate(PostResponseEvent $event) {
  $operation_records = $this->rngEntityModel
    ->getOperationRecords();
  foreach ($operation_records as $operation_record) {
    if ($operation_record
      ->getEntityTypeId() == 'registration') {

      /** @var \Drupal\rng\Entity\RegistrationInterface $registration */
      $registration = $this->entityTypeManager
        ->getStorage('registration')
        ->load($operation_record
        ->getEntityId());
      if (!$registration) {

        // Registration no longer exists, need full object to act on.
        // @todo: if entity is about to be deleted, then all existing
        // operation records should be processed in preDelete.
        continue;
      }
      switch ($operation_record
        ->getOperation()) {
        case 'insert':
          $trigger_id = 'entity:registration:new';
          break;
        case 'update':
          $trigger_id = 'entity:registration:update';
          break;
      }
      if (isset($trigger_id)) {
        $event_meta = $this->eventManager
          ->getMeta($registration
          ->getEvent());
        $event_meta
          ->trigger($trigger_id, [
          'registrations' => [
            $registration,
          ],
        ]);
      }
    }
  }
}