abstract class MessageNotifierBase in Message Notify 8
An abstract implementation of MessageNotifierInterface.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\message_notify\Plugin\Notifier\MessageNotifierBase implements MessageNotifierInterface
Expanded class hierarchy of MessageNotifierBase
1 file declares its use of MessageNotifierBase
- MessageNotifierTest.php in tests/
modules/ message_notify_test/ src/ Plugin/ Notifier/ MessageNotifierTest.php
File
- src/
Plugin/ Notifier/ MessageNotifierBase.php, line 16
Namespace
Drupal\message_notify\Plugin\NotifierView source
abstract class MessageNotifierBase extends PluginBase implements MessageNotifierInterface {
/**
* The message entity.
*
* @var \Drupal\message\MessageInterface
*/
protected $message;
/**
* The logger channel.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The rendering service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Constructs the plugin.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Logger\LoggerChannelInterface $logger
* The message_notify logger channel.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The rendering service.
* @param \Drupal\message\MessageInterface $message
* (optional) The message entity. This is required when sending or
* delivering a notification. If not passed to the constructor, use
* ::setMessage().
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelInterface $logger, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, MessageInterface $message = NULL) {
// Set some defaults.
$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;
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*
* - Save the rendered messages if needed.
* - Invoke watchdog error on failure.
*/
public function postSend($result, array $output = []) {
$save = FALSE;
// NULL means skip delivery. False signifies failure. Strict check.
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];
// @todo Inject the content_type.manager if this check is needed.
if (!($field = $this->entityTypeManager
->getStorage('field_config')
->load('message.' . $this->message
->bundle() . '.' . $field_name))) {
throw new MessageNotifyException('Field "' . $field_name . '"" does not exist.');
}
// Get the format from the field. We assume the first delta is the
// same as the rest.
if (!($format = $this->message
->get($field_name)->format)) {
// Field has no formatting.
// @todo Centralize/unify rendering.
$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();
}
}
/**
* {@inheritdoc}
*/
public function access() {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function setMessage(MessageInterface $message) {
$this->message = $message;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MessageNotifierBase:: |
protected | property | The entity type manager service. | |
MessageNotifierBase:: |
protected | property | The logger channel. | |
MessageNotifierBase:: |
protected | property | The message entity. | |
MessageNotifierBase:: |
protected | property | The rendering service. | |
MessageNotifierBase:: |
public | function |
Determine if user can access notifier. Overrides MessageNotifierInterface:: |
|
MessageNotifierBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
MessageNotifierBase:: |
public | function |
Save the rendered messages if needed.
Invoke watchdog error on failure. Overrides MessageNotifierInterface:: |
|
MessageNotifierBase:: |
public | function |
Entry point to send and process a message. Overrides MessageNotifierInterface:: |
|
MessageNotifierBase:: |
public | function |
Set the message object for the notifier. Overrides MessageNotifierInterface:: |
|
MessageNotifierBase:: |
public | function |
Constructs the plugin. Overrides PluginBase:: |
1 |
MessageNotifierInterface:: |
public | function | Deliver a message via the required transport method. | 3 |
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. |