message-8.x-1.1/src/MessageListBuilder.php
src/MessageListBuilder.php
<?php
namespace Drupal\message;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a class to build a listing of Message entities.
*
* @see \Drupal\Message\Entity\Message
*/
final class MessageListBuilder extends EntityListBuilder {
/**
* The date service.
*
* @var \Drupal\Core\Datetime\DateFormatter
*/
protected $dateService;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Creates a Message ListBuilder.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\Core\Datetime\DateFormatter $date_service
* The date service.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(
EntityTypeInterface $entity_type,
EntityStorageInterface $storage,
DateFormatter $date_service,
TranslationInterface $string_translation,
LanguageManagerInterface $language_manager,
) {
parent::__construct($entity_type, $storage);
$this->dateService = $date_service;
$this->languageManager = $language_manager;
$this->setStringTranslation($string_translation);
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new self(
$entity_type,
$container->get('entity_type.manager')->getStorage($entity_type->id()),
$container->get('date.formatter'),
$container->get('string_translation'),
$container->get('language_manager')
);
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
// Enable language column and filter if multiple languages are added.
$header = [
'created' => [
'data' => $this->t('Created'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
'text' => $this->t('Text'),
'template' => [
'data' => $this->t('Template'),
'class' => [RESPONSIVE_PRIORITY_MEDIUM],
],
'author' => [
'data' => $this->t('Author'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
];
if ($this->languageManager->isMultilingual()) {
$header['language_name'] = [
'data' => $this->t('Language'),
'class' => [RESPONSIVE_PRIORITY_LOW],
];
}
return $header;
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/** @var Message $entity */
$text = $entity->getText();
return [
'changed' => $this->dateService->format($entity->getCreatedTime(), 'short'),
'text' => [
'data' => [
'#markup' => reset($text),
],
],
'template' => $entity->getTemplate()->label(),
'author' => (!empty($entity->getOwner())) ? $entity->getOwner()->label() : $this->t('Anonymous'),
];
}
}
