You are here

public function DigestFormatter::format in Message Digest 8

Build the full message digest content for a given set of messages.

Parameters

\Drupal\message\MessageInterface[] $digest: An array of messages.

array $view_modes: An array of view modes to build.

\Drupal\user\UserInterface $recipient: The digest recipient.

Return value

string Fully rendered message digest.

Overrides DigestFormatterInterface::format

File

src/DigestFormatter.php, line 44

Class

DigestFormatter
Message digest formatter service.

Namespace

Drupal\message_digest

Code

public function format(array $digest, array $view_modes, UserInterface $recipient) {
  $output = [
    '#theme' => 'message_digest',
    '#messages' => [],
  ];
  foreach ($digest as $message) {

    // Set the user to the recipient. This is similar to how message_subscribe
    // works when sending a message to many different users.
    $message
      ->setOwner($recipient);
    $rows = [
      '#theme' => 'message_digest_rows',
      '#message' => $message,
    ];
    foreach ($view_modes as $view_mode) {
      $build = $this->entityTypeManager
        ->getViewBuilder('message')
        ->view($message, $view_mode);
      $rows[] = $build;
    }
    $output[] = $rows;
  }
  return $this->renderer
    ->renderPlain($output);
}