bat-8.x-1.x-dev/modules/bat_event/src/EventListBuilder.php
modules/bat_event/src/EventListBuilder.php
<?php
namespace Drupal\bat_event;
use Drupal\Core\Config\ConfigFactoryInterface;
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 Drupal\Core\Routing\RedirectDestinationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a class to build a listing of Event entities.
*
* @ingroup bat
*/
class EventListBuilder extends EntityListBuilder {
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* The redirect destination service.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface
*/
protected $redirectDestination;
/**
* Constructs a new EventListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
* @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
* The redirect destination service.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
parent::__construct($entity_type, $storage);
$this->configFactory = $config_factory;
$this->dateFormatter = $date_formatter;
$this->redirectDestination = $redirect_destination;
}
/**
* {@inheritdoc}
*/
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('config.factory'),
$container->get('date.formatter'),
$container->get('redirect.destination')
);
}
/**
* {@inheritdoc}
*/
public function load() {
$entity_query = $this->storage->getQuery();
$entity_query->accessCheck(TRUE);
$entity_query->pager(50);
$header = $this->buildHeader();
$entity_query->tableSort($header);
$uids = $entity_query->execute();
return $this->storage->loadMultiple($uids);
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header = [
'id' => [
'data' => $this->t('Event ID'),
'field' => 'id',
'specifier' => 'id',
'class' => [RESPONSIVE_PRIORITY_LOW],
],
'start_date' => [
'data' => $this->t('Start Date'),
'field' => 'event_dates',
'specifier' => 'event_dates',
'class' => [RESPONSIVE_PRIORITY_LOW],
],
'end_date' => [
'data' => $this->t('End Date'),
'field' => 'event_dates',
'specifier' => 'event_dates',
'class' => [RESPONSIVE_PRIORITY_LOW],
],
'type' => [
'data' => $this->t('Type'),
'field' => 'type',
'specifier' => 'type',
'class' => [RESPONSIVE_PRIORITY_LOW],
],
];
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$date_format = $this->configFactory->get('bat.settings')->get('bat_date_format') ?: 'Y-m-d H:i';
$row['id'] = $entity->id();
$row['start_date'] = $entity->getStartDate()->format($date_format);
$row['end_date'] = $entity->getEndDate()->format($date_format);
$row['type'] = bat_event_type_load($entity->bundle())->label();
return $row + parent::buildRow($entity);
}
}
