You are here

abstract class MessageNotifierBase in Message Notify 8

An abstract implementation of MessageNotifierInterface.

Hierarchy

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\Notifier
View 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

Namesort descending Modifiers Type Description Overrides
MessageNotifierBase::$entityTypeManager protected property The entity type manager service.
MessageNotifierBase::$logger protected property The logger channel.
MessageNotifierBase::$message protected property The message entity.
MessageNotifierBase::$renderer protected property The rendering service.
MessageNotifierBase::access public function Determine if user can access notifier. Overrides MessageNotifierInterface::access
MessageNotifierBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
MessageNotifierBase::postSend public function Save the rendered messages if needed. Invoke watchdog error on failure. Overrides MessageNotifierInterface::postSend
MessageNotifierBase::send public function Entry point to send and process a message. Overrides MessageNotifierInterface::send
MessageNotifierBase::setMessage public function Set the message object for the notifier. Overrides MessageNotifierInterface::setMessage
MessageNotifierBase::__construct public function Constructs the plugin. Overrides PluginBase::__construct 1
MessageNotifierInterface::deliver public function Deliver a message via the required transport method. 3
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.