View source
<?php
namespace Drupal\workbench_email\Plugin\QueueWorker;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Utility\Token;
use Drupal\workbench_email\QueuedEmail;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WorkbenchEmailProcessor extends QueueWorkerBase implements ContainerFactoryPluginInterface {
protected $targetEntityType;
protected $mailManager;
protected $renderer;
protected $token;
protected $entityRepository;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MailManagerInterface $mail_manager, EntityRepositoryInterface $entity_repository, Token $token, RendererInterface $renderer, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->mailManager = $mail_manager;
$this->targetEntityType = $plugin_definition['entity_type'];
$this->entityRepository = $entity_repository;
$this->token = $token;
$this->renderer = $renderer;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.mail'), $container
->get('entity.repository'), $container
->get('token'), $container
->get('renderer'), $container
->get('entity_type.manager'));
}
public function processItem($data) {
if ($data instanceof QueuedEmail) {
$template = $data
->getTemplate();
$uuid = $data
->getUuid();
if ($entity = $this->entityRepository
->loadEntityByUuid($this->targetEntityType, $uuid)) {
$body = $template
->getBody();
$entity_storage = $this->entityTypeManager
->getStorage($this->targetEntityType);
$latest_revision_id = $entity_storage
->getLatestRevisionId($entity
->id());
$entity = $entity_storage
->loadRevision($latest_revision_id);
$subject = $this->token
->replace($template
->getSubject(), [
$entity
->getEntityTypeId() => $entity,
]);
$body['value'] = $this->token
->replace($body['value'], [
$entity
->getEntityTypeId() => $entity,
]);
$body = $this
->checkMarkup($body['value'], $body['format']);
$replyTo = !empty($template
->getReplyTo()) ? $this->token
->replace($template
->getReplyTo(), [
$entity
->getEntityTypeId() => $entity,
]) : NULL;
$this->mailManager
->mail('workbench_email', 'template::' . $template
->id(), $data
->getTo(), LanguageInterface::LANGCODE_DEFAULT, [
'body' => $body,
'template' => $template,
'subject' => $subject,
], $replyTo);
}
}
else {
throw new \InvalidArgumentException('Cannot perform queue processing on objects other than a QueuedEmail.');
}
}
protected function checkMarkup($text, $format_id, $langcode = LanguageInterface::LANGCODE_DEFAULT) {
$build = [
'#type' => 'processed_text',
'#text' => $text,
'#format' => $format_id,
'#filter_types_to_skip' => [],
'#langcode' => $langcode,
];
return $this->renderer
->renderPlain($build);
}
}