You are here

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

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

Event handler for Workbench Moderation.

Parameters

\Drupal\workbench_moderation\Event\WorkbenchModerationTransitionEvent $event: The event listened to.

File

src/EventSubscriber/WorkbenchTransitionEventSubscriber.php, line 55

Class

WorkbenchTransitionEventSubscriber
Subscribes to transition changes to send notification emails.

Namespace

Drupal\workbench_email\EventSubscriber

Code

public function onWorkbenchModerationTransition($event) {
  $entity = $event
    ->getEntity();

  /* @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle_entity */
  $bundle_entity = $this->entityTypeManager
    ->getStorage($entity
    ->getEntityType()
    ->getBundleEntityType())
    ->load($entity
    ->bundle());
  if (!$event
    ->getStateBefore()) {

    // We need to use the default.
    $from = $bundle_entity
      ->getThirdPartySetting('workbench_moderation', 'default_moderation_state', FALSE);
  }
  else {
    $from = $event
      ->getStateBefore();
  }
  $to = $event
    ->getStateAfter();

  // Load transitions.
  // We don't have the transition available, so we have to load any matching
  // ones.
  if ($transitions = $this->entityTypeManager
    ->getStorage('moderation_state_transition')
    ->loadByProperties([
    'stateFrom' => $from,
    'stateTo' => $to,
  ])) {

    // Filter out any that the user doesn't have access to or that don't have
    // any email templates.
    $transitions = array_filter($transitions, function ($transition) {

      /** @var \Drupal\workbench_moderation\ModerationStateTransitionInterface $transition */
      return $this->currentUser
        ->hasPermission(sprintf('use %s transition', $transition
        ->id())) && $transition
        ->getThirdPartySetting('workbench_email', 'workbench_email_templates', []);
    });
    if (!$transitions) {

      // None remain, nothing to do.
      return;
    }

    // There may be multiple at this point, but given we don't have access
    // to the transition that fired this event, we just pick the first one.
    $transition = reset($transitions);

    /** @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($transition
      ->getThirdPartySetting('workbench_email', 'workbench_email_templates', [])) 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));
      }
    }
  }
}