ScheduledTransitionsListBuilder.php in Scheduled Transitions 8
File
src/ScheduledTransitionsListBuilder.php
View source
<?php
declare (strict_types=1);
namespace Drupal\scheduled_transitions;
use Drupal\Component\Serialization\Json;
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\Entity\Exception\UndefinedLinkTemplateException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ScheduledTransitionsListBuilder extends EntityListBuilder {
protected $dateFormatter;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $dateFormatter) {
parent::__construct($entity_type, $storage);
$this->dateFormatter = $dateFormatter;
}
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 load() {
$query = $this->storage
->getQuery();
$header = $this
->buildHeader();
$query
->tableSort($header);
$ids = $query
->execute();
return $this->storage
->loadMultiple($ids);
}
public function buildHeader() : array {
$header = [
'entity' => $this
->t('Entity'),
'date' => [
'data' => $this
->t('On date'),
'field' => 'transition_on',
'specifier' => 'transition_on',
'sort' => 'asc',
],
] + parent::buildHeader();
return $header;
}
public function buildRow(EntityInterface $entity) : array {
$row = [];
$hostEntity = $entity
->getEntity();
try {
$row['host_entity'] = $hostEntity ? $hostEntity
->toLink() : $this
->t('- Missing entity -');
} catch (UndefinedLinkTemplateException $exception) {
$row['host_entity'] = $hostEntity
->label();
}
$time = $entity
->getTransitionTime();
$row['date'] = $this->dateFormatter
->format($time);
return $row + parent::buildRow($entity);
}
protected function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
$rescheduleUrl = $entity
->toUrl('reschedule-form');
if ($rescheduleUrl
->access()) {
$operations['reschedule'] = [
'title' => $this
->t('Reschedule'),
'weight' => 20,
'url' => $this
->ensureDestination($rescheduleUrl),
'attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 500,
]),
],
];
}
return $operations;
}
public function buildOperations(EntityInterface $entity) {
$build = parent::buildOperations($entity);
$build['#cache']['contexts'][] = 'user.permissions';
return $build;
}
}