View source
<?php
namespace Drupal\message_notify\Plugin\Notifier;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\message\MessageInterface;
use Drupal\message_notify\Exception\MessageNotifyException;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class MessageNotifierBase extends PluginBase implements MessageNotifierInterface {
protected $message;
protected $logger;
protected $entityTypeManager;
protected $renderer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelInterface $logger, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, MessageInterface $message = NULL) {
$configuration += [
'save on success' => TRUE,
'save on fail' => FALSE,
];
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->logger = $logger;
$this->entityTypeManager = $entity_type_manager;
$this->message = $message;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MessageInterface $message = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('logger.channel.message_notify'), $container
->get('entity_type.manager'), $container
->get('renderer'), $message);
}
public function send() {
$has_message = isset($this->message);
assert($has_message, 'No message is set for this notifier.');
$output = [];
$view_builder = $this->entityTypeManager
->getViewBuilder('message');
foreach ($this->pluginDefinition['viewModes'] as $view_mode) {
$build = $view_builder
->view($this->message, $view_mode);
$output[$view_mode] = $this->renderer
->renderPlain($build);
}
$result = $this
->deliver($output);
$this
->postSend($result, $output);
return $result;
}
public function postSend($result, array $output = []) {
$save = FALSE;
if ($result === FALSE) {
$this->logger
->error('Could not send message using {title} to user ID {uid}.', [
'{title}' => $this->pluginDefinition['title'],
'{uid}' => $this->message
->getOwnerId(),
]);
if ($this->configuration['save on fail']) {
$save = TRUE;
}
}
elseif ($result !== FALSE && $this->configuration['save on success']) {
$save = TRUE;
}
if (isset($this->configuration['rendered fields'])) {
foreach ($this->pluginDefinition['viewModes'] as $view_mode) {
if (empty($this->configuration['rendered fields'][$view_mode])) {
throw new MessageNotifyException('The rendered view mode "' . $view_mode . '" cannot be saved to field, as there is not a matching one.');
}
$field_name = $this->configuration['rendered fields'][$view_mode];
if (!($field = $this->entityTypeManager
->getStorage('field_config')
->load('message.' . $this->message
->bundle() . '.' . $field_name))) {
throw new MessageNotifyException('Field "' . $field_name . '"" does not exist.');
}
if (!($format = $this->message
->get($field_name)->format)) {
$this->message
->set($field_name, $output[$view_mode]);
}
else {
$this->message
->set($field_name, [
'value' => $output[$view_mode],
'format' => $format,
]);
}
}
}
if ($save) {
$this->message
->save();
}
}
public function access() {
return TRUE;
}
public function setMessage(MessageInterface $message) {
$this->message = $message;
}
}