View source
<?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 Symfony\Component\DependencyInjection\ContainerInterface;
class MessageListBuilder extends EntityListBuilder {
protected $dateService;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatter $date_service) {
parent::__construct($entity_type, $storage);
$this->dateService = $date_service;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager')
->getStorage($entity_type
->id()), $container
->get('date.formatter'));
}
public function buildHeader() {
$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 (\Drupal::languageManager()
->isMultilingual()) {
$header['language_name'] = [
'data' => $this
->t('Language'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
];
}
return $header;
}
public function buildRow(EntityInterface $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'),
];
}
}