recurring_events-2.0.x-dev/src/EventInstanceListBuilder.php
src/EventInstanceListBuilder.php
<?php
declare(strict_types=1);
namespace Drupal\recurring_events;
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\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a listing of eventinstance items.
*/
class EventInstanceListBuilder extends EntityListBuilder {
public function __construct(
EntityTypeInterface $entity_type,
EntityStorageInterface $storage,
protected readonly DateFormatterInterface $dateFormatter,
protected readonly LanguageManagerInterface $languageManager,
protected readonly ConfigFactoryInterface $configFactory,
) {
parent::__construct($entity_type, $storage);
$this->limit = $this->configFactory->get('recurring_events.eventinstance.config')->get('limit');
}
/**
* {@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('date.formatter'),
$container->get('language_manager'),
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header = [];
$header += [
'name' => $this->t('Event Name'),
'series' => [
'data' => $this->t('Series'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
'date' => $this->t('Date'),
'author' => [
'data' => $this->t('Author'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
'status' => $this->t('Status'),
'changed' => [
'data' => $this->t('Updated'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
];
// Enable language column if multiple languages are added.
if ($this->languageManager->isMultilingual()) {
$header['language'] = [
'data' => $this->t('Language'),
'class' => [RESPONSIVE_PRIORITY_LOW],
];
}
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/** @var \Drupal\recurring_events\EventInterface $entity */
$row['name']['data'] = [
'#type' => 'link',
'#title' => $entity->getEventSeries()->label(),
'#url' => $entity->toUrl(),
];
$row['series']['data'] = [
'#type' => 'link',
'#title' => $this->t('View Series'),
'#url' => $entity->getEventSeries()->toUrl(),
];
$config = $this->configFactory->get('recurring_events.eventinstance.config');
$timezone = new \DateTimeZone(date_default_timezone_get());
$entity->date->start_date->setTimezone($timezone);
$row['date'] = $entity->date->start_date->format($config->get('date_format'));
$row['author']['data'] = [
'#theme' => 'username',
'#account' => $entity->getOwner(),
];
$row['status'] = $entity->isPublished() ? $this->t('Published') : $this->t('Unpublished');
$row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short', '', $timezone->getName());
if ($this->languageManager->isMultilingual()) {
$row['language'] = $this->languageManager->getLanguageName($entity->language()->getId());
}
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
protected function getEntityIds() {
$query = $this->getStorage()->getQuery()->accessCheck(TRUE)
->sort('date__value', 'ASC');
// Only add the pager if a limit is specified.
if ($this->limit) {
$query->pager($this->limit);
}
return $query->accessCheck(FALSE)->execute();
}
}
