You are here

public function DigestBase::deliver in Message Digest 8

Deliver a message via the required transport method.

Parameters

array $output: Array keyed by the view mode, and the rendered entity in the specified view mode.

Return value

bool TRUE or FALSE based on delivery status.

Overrides MessageNotifierInterface::deliver

File

src/Plugin/Notifier/DigestBase.php, line 111

Class

DigestBase
Message Digest notifier.

Namespace

Drupal\message_digest\Plugin\Notifier

Code

public function deliver(array $output = []) {

  // Do not actually deliver this message because it will be delivered
  // via cron in a digest, but return TRUE to prevent a logged error.
  // Instead, we "deliver" it to the message_digest DB table so that it
  // can be retrieved at a later time.
  $message = $this->message;
  $message_digest = [
    'receiver' => $message
      ->getOwnerId(),
    'entity_type' => $this->configuration['entity_type'],
    'entity_id' => $this->configuration['entity_id'],
    'notifier' => $this
      ->getPluginId(),
    'timestamp' => $message
      ->getCreatedTime(),
  ];

  // Don't allow entity_id without entity_type, or the reverse.
  if ($this->configuration['entity_type'] xor $this->configuration['entity_id']) {
    throw new InvalidDigestGroupingException(sprintf('Tried to create a message digest without both entity_type (%s) and entity_id (%s). These either both need to be empty, or have values.', $this->configuration['entity_type'], $this->configuration['entity_id']));
  }

  // Our $message is a cloned copy of the original $message with the mid field
  // removed to prevent overwriting (this happens in message_subscribe) so we
  // need to fetch the mid manually.
  $mid = $message
    ->id();
  if (!$mid && isset($message->original_message)) {
    $mid = $message->original_message
      ->id();
  }
  assert(!empty($mid), 'The message entity (or $message->original_message) must be saved in order to create a digest entry.');
  $message_digest['mid'] = $mid;
  $this->connection
    ->insert('message_digest')
    ->fields($message_digest)
    ->execute();
  return TRUE;
}