You are here

public function MessageNotifierBase::postSend in Message Notify 8

  • Save the rendered messages if needed.
  • Invoke watchdog error on failure.

Overrides MessageNotifierInterface::postSend

1 call to MessageNotifierBase::postSend()
MessageNotifierBase::send in src/Plugin/Notifier/MessageNotifierBase.php
Entry point to send and process a message.

File

src/Plugin/Notifier/MessageNotifierBase.php, line 122

Class

MessageNotifierBase
An abstract implementation of MessageNotifierInterface.

Namespace

Drupal\message_notify\Plugin\Notifier

Code

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();
  }
}