You are here

public function WorkbenchTransitionEventSubscriber::onContentModerationTransition in Workbench Email 2.x

Same name and namespace in other branches
  1. 8 src/EventSubscriber/WorkbenchTransitionEventSubscriber.php \Drupal\workbench_email\EventSubscriber\WorkbenchTransitionEventSubscriber::onContentModerationTransition()

Event handler for Content Moderation.

Parameters

\Drupal\content_moderation\Event\ContentModerationStateChangedEvent|\Drupal\workbench_email\EventSubscriber\ContentModerationStateChangedEvent $event: The event listened to.

File

src/EventSubscriber/WorkbenchTransitionEventSubscriber.php, line 109

Class

WorkbenchTransitionEventSubscriber
Subscribes to transition changes to send notification emails.

Namespace

Drupal\workbench_email\EventSubscriber

Code

public function onContentModerationTransition($event) {
  $entity = $event
    ->getModeratedEntity();

  /** @var \Drupal\workflows\WorkflowInterface $workflow */
  $workflow = $this->entityTypeManager
    ->getStorage('workflow')
    ->load($event
    ->getWorkflow());

  /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModerationInterface $type_plugin */
  $type_plugin = $workflow
    ->getTypePlugin();
  if (!$event
    ->getOriginalState()) {
    $from = $type_plugin
      ->getInitialState($entity)
      ->id();
  }
  else {
    $from = $event
      ->getOriginalState();
  }
  $to = $event
    ->getNewState();
  $templates = $workflow
    ->getThirdPartySetting('workbench_email', 'workbench_email_templates', []);
  try {
    $transition = $type_plugin
      ->getTransitionFromStateToState($from, $to);
  } catch (\InvalidArgumentException $e) {

    // Do nothing in case of invalid transition.
    return;
  }

  // Check the transition has a template
  if (!isset($templates[$transition
    ->id()])) {
    return;
  }

  /** @var \Drupal\Core\Queue\QueueInterface $queue */
  $queue = $this->queueFactory
    ->get('workbench_email_send' . PluginBase::DERIVATIVE_SEPARATOR . $entity
    ->getEntityTypeId());

  /** @var \Drupal\workbench_email\TemplateInterface $template */
  foreach ($this->entityTypeManager
    ->getStorage('workbench_email_template')
    ->loadMultiple($templates[$transition
    ->id()]) as $template) {
    if ($template
      ->getBundles() && !in_array($entity
      ->getEntityTypeId() . ':' . $entity
      ->bundle(), $template
      ->getBundles(), TRUE)) {

      // Continue, invalid bundle.
      continue;
    }
    foreach ($this
      ->prepareRecipients($entity, $template) as $to) {
      $queue
        ->createItem(new QueuedEmail($template, $entity
        ->uuid(), $to));
    }
  }
}