View source
<?php
namespace Drupal\mass_contact;
use Drupal\Core\Datetime\DateFormatterInterface;
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 $dateFormatter;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter) {
parent::__construct($entity_type, $storage);
$this->dateFormatter = $date_formatter;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity.manager')
->getStorage($entity_type
->id()), $container
->get('date.formatter'));
}
public function buildHeader() {
$header = [
'subject' => [
'data' => $this
->t('Subject'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
],
'sent' => [
'data' => $this
->t('Sent on'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
],
'uid' => [
'data' => $this
->t('Sent by'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
'categories' => [
'data' => $this
->t('Categories'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
];
$header += parent::buildHeader();
return $header;
}
public function buildRow(EntityInterface $entity) {
$categories = [];
foreach ($entity
->getCategories() as $category) {
if (!empty($category)) {
$categories[] = $category
->label();
}
}
$row = [
'subject' => $entity
->toLink(),
'sent' => $this->dateFormatter
->format($entity
->getSentTime(), 'short'),
'uid' => $entity
->getOwner()
->toLink(),
'categories' => [
'data' => [
'#theme' => 'item_list',
'#items' => $categories,
],
],
];
$row += parent::buildRow($entity);
return $row;
}
public function getDefaultOperations(EntityInterface $entity) {
if ($entity
->access('view') && $entity
->hasLinkTemplate('canonical')) {
$operations['view'] = [
'title' => $this
->t('View'),
'weight' => 10,
'url' => $entity
->toUrl(),
];
}
return $operations;
}
}