You are here

public function SchedulerManager::dispatchSchedulerEvent in Scheduler 2.x

Dispatches a Scheduler event for an entity.

This function dispatches a Scheduler event, identified by $event_id, for the entity type of the provided $entity. Each entity type has its own events class Scheduler{EntityType}Events, for example SchedulerNodeEvents, SchedulerMediaEvents, etc. This class contains constants (with names matching the $event_id parameter) which uniquely define the final event name string to be dispatched. The actual event object dispatched is always of class SchedulerEvent.

The $entity is passed by reference so that any changes made in the event subscriber implementations are automatically stored and passed forward.

Parameters

Drupal\Core\Entity\EntityInterface $entity: The entity object.

string $event_id: The short text id the event, for example 'PUBLISH' or 'PRE_UNPUBLISH'.

2 calls to SchedulerManager::dispatchSchedulerEvent()
SchedulerManager::publish in src/SchedulerManager.php
Publish scheduled entities.
SchedulerManager::unpublish in src/SchedulerManager.php
Unpublish scheduled entities.

File

src/SchedulerManager.php, line 167

Class

SchedulerManager
Defines a scheduler manager.

Namespace

Drupal\scheduler

Code

public function dispatchSchedulerEvent(EntityInterface &$entity, string $event_id) {

  // Get the fully named-spaced event class name for the entity type, for use
  // in the constant() function.
  $event_class = $this
    ->getPlugin($entity
    ->getEntityTypeId())
    ->schedulerEventClass();
  $event_name = constant("{$event_class}::{$event_id}");

  // Create the event object and dispatch the required event_name.
  $event = new SchedulerEvent($entity);
  $this
    ->dispatch($event, $event_name);

  // Get the entity, as it may have been modified by an event subscriber.
  $entity = $event
    ->getEntity();
}