class WorkbenchEmailProcessor in Workbench Email 8
Same name in this branch
- 8 src/WorkbenchEmailProcessor.php \Drupal\workbench_email\WorkbenchEmailProcessor
- 8 src/Plugin/QueueWorker/WorkbenchEmailProcessor.php \Drupal\workbench_email\Plugin\QueueWorker\WorkbenchEmailProcessor
Same name and namespace in other branches
- 2.x src/Plugin/QueueWorker/WorkbenchEmailProcessor.php \Drupal\workbench_email\Plugin\QueueWorker\WorkbenchEmailProcessor
Sends emails notifications for workbench events.
Plugin annotation
@QueueWorker(
id = "workbench_email_send",
title = @Translation("Sends workbench email notifications"),
cron = {"time" = 60},
deriver = "\Drupal\workbench_email\Plugin\Derivative\WorkbenchEmailDeriver"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Queue\QueueWorkerBase implements QueueWorkerInterface
- class \Drupal\workbench_email\Plugin\QueueWorker\WorkbenchEmailProcessor implements ContainerFactoryPluginInterface
- class \Drupal\Core\Queue\QueueWorkerBase implements QueueWorkerInterface
Expanded class hierarchy of WorkbenchEmailProcessor
File
- src/
Plugin/ QueueWorker/ WorkbenchEmailProcessor.php, line 26
Namespace
Drupal\workbench_email\Plugin\QueueWorkerView source
class WorkbenchEmailProcessor extends QueueWorkerBase implements ContainerFactoryPluginInterface {
/**
* Entity type ID.
*
* @var string
*/
protected $targetEntityType;
/**
* Mail manager service.
*
* @var \Drupal\Core\Mail\MailManager
*/
protected $mailManager;
/**
* Renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* Entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new WorkbenchEmailProcessor object.
*
* @param array $configuration
* Configuration.
* @param string $plugin_id
* Plugin ID.
* @param mixed $plugin_definition
* Definition.
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
* Mail manager service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* Entity repository service.
* @param \Drupal\Core\Utility\Token $token
* Token service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* Renderer service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
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();
/* @var \Drupal\Core\Entity\RevisionableStorageInterface $entity_storage */
$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;
// Send the email.
$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.');
}
}
/**
* Converts message body to markup applying filter.
*
* @param string $text
* Text to filter.
* @param string $format_id
* Format ID.
* @param string $langcode
* Langcode.
*
* @return \Drupal\Component\Render\MarkupInterface
* Filtered markup.
*/
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
WorkbenchEmailProcessor:: |
protected | property | Entity repository service. | |
WorkbenchEmailProcessor:: |
protected | property | Entity type manager service. | |
WorkbenchEmailProcessor:: |
protected | property | Mail manager service. | |
WorkbenchEmailProcessor:: |
protected | property | Renderer. | |
WorkbenchEmailProcessor:: |
protected | property | Entity type ID. | |
WorkbenchEmailProcessor:: |
protected | property | Token service. | |
WorkbenchEmailProcessor:: |
protected | function | Converts message body to markup applying filter. | |
WorkbenchEmailProcessor:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
WorkbenchEmailProcessor:: |
public | function |
Works on a single queue item. Overrides QueueWorkerInterface:: |
|
WorkbenchEmailProcessor:: |
public | function |
Constructs a new WorkbenchEmailProcessor object. Overrides PluginBase:: |