3 namespace Drupal\{{ machine_name }};
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityListBuilder;
7 use Drupal\Core\Datetime\DateFormatterInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Routing\RedirectDestinationInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides a list controller for the {{ entity_type_label|lower }} entity type.
16 class {{ class_prefix }}ListBuilder extends EntityListBuilder {
19 * The date formatter service.
21 * @var \Drupal\Core\Datetime\DateFormatterInterface
23 protected $dateFormatter;
26 * The redirect destination service.
28 * @var \Drupal\Core\Routing\RedirectDestinationInterface
30 protected $redirectDestination;
33 * Constructs a new {{ class_prefix }}ListBuilder object.
35 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
36 * The entity type definition.
37 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
38 * The entity storage class.
39 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
40 * The date formatter service.
41 * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
42 * The redirect destination service.
44 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
45 parent::__construct($entity_type, $storage);
46 $this->dateFormatter = $date_formatter;
47 $this->redirectDestination = $redirect_destination;
53 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
56 $container->get('entity_type.manager')->getStorage($entity_type->id()),
57 $container->get('date.formatter'),
58 $container->get('redirect.destination')
65 public function render() {
66 $build['table'] = parent::render();
68 $total = \Drupal::database()
69 ->query('SELECT COUNT(*) FROM {{ '{' }}{{ entity_type_id }}{{ '}' }}')
72 $build['summary']['#markup'] = $this->t('Total {{ entity_type_label|lower|plural }}: @total', ['@total' => $total]);
79 public function buildHeader() {
80 $header['id'] = $this->t('ID');
81 {% if title_base_field %}
82 $header['title'] = $this->t('Title');
84 {% if status_base_field %}
85 $header['status'] = $this->t('Status');
87 {% if author_base_field %}
88 $header['uid'] = $this->t('Author');
90 {% if created_base_field %}
91 $header['created'] = $this->t('Created');
93 {% if changed_base_field %}
94 $header['changed'] = $this->t('Updated');
96 return $header + parent::buildHeader();
102 public function buildRow(EntityInterface $entity) {
103 /* @var $entity \Drupal\{{ machine_name }}\Entity\{{ class_prefix }} */
104 $row['id'] = $entity->{{ title_base_field ? 'id' : 'link' }}();
105 {% if title_base_field %}
106 $row['title'] = $entity->link();
108 {% if status_base_field %}
109 $row['status'] = $entity->isEnabled() ? $this->t('Enabled') : $this->t('Disabled');
111 {% if author_base_field %}
112 $row['uid']['data'] = [
113 '#theme' => 'username',
114 '#account' => $entity->getOwner(),
117 {% if created_base_field %}
118 $row['created'] = $this->dateFormatter->format($entity->getCreatedTime());
120 {% if changed_base_field %}
121 $row['changed'] = $this->dateFormatter->format($entity->getChangedTime());
123 return $row + parent::buildRow($entity);
129 protected function getDefaultOperations(EntityInterface $entity) {
130 $operations = parent::getDefaultOperations($entity);
131 $destination = $this->redirectDestination->getAsArray();
132 foreach ($operations as $key => $operation) {
133 $operations[$key]['query'] = $destination;