DigestFormatter.php in Message Digest 8
File
src/DigestFormatter.php
View source
<?php
namespace Drupal\message_digest;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\user\UserInterface;
class DigestFormatter implements DigestFormatterInterface {
protected $entityTypeManager;
protected $renderer;
public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer) {
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
}
public function format(array $digest, array $view_modes, UserInterface $recipient) {
$output = [
'#theme' => 'message_digest',
'#messages' => [],
];
foreach ($digest as $message) {
$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);
}
}