You are here

class DigestFormatter in Message Digest 8

Message digest formatter service.

Hierarchy

Expanded class hierarchy of DigestFormatter

1 string reference to 'DigestFormatter'
message_digest.services.yml in ./message_digest.services.yml
message_digest.services.yml
1 service uses DigestFormatter
message_digest.formatter in ./message_digest.services.yml
Drupal\message_digest\DigestFormatter

File

src/DigestFormatter.php, line 12

Namespace

Drupal\message_digest
View source
class DigestFormatter implements DigestFormatterInterface {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Construct the digest formatter service.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The rendering service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer) {
    $this->entityTypeManager = $entity_type_manager;
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DigestFormatter::$entityTypeManager protected property The entity type manager service.
DigestFormatter::$renderer protected property The renderer service.
DigestFormatter::format public function Build the full message digest content for a given set of messages. Overrides DigestFormatterInterface::format
DigestFormatter::__construct public function Construct the digest formatter service.