0030494221d8cd286a002ab1c1dad0f0d07a7aeb
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / module / content-entity / src / ExampleListBuilder.php.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }};
4
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;
12
13 /**
14  * Provides a list controller for the {{ entity_type_label|lower }} entity type.
15  */
16 class {{ class_prefix }}ListBuilder extends EntityListBuilder {
17
18   /**
19    * The date formatter service.
20    *
21    * @var \Drupal\Core\Datetime\DateFormatterInterface
22    */
23   protected $dateFormatter;
24
25   /**
26    * The redirect destination service.
27    *
28    * @var \Drupal\Core\Routing\RedirectDestinationInterface
29    */
30   protected $redirectDestination;
31
32   /**
33    * Constructs a new {{ class_prefix }}ListBuilder object.
34    *
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.
43    */
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;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
54     return new static(
55       $entity_type,
56       $container->get('entity_type.manager')->getStorage($entity_type->id()),
57       $container->get('date.formatter'),
58       $container->get('redirect.destination')
59     );
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function render() {
66     $build['table'] = parent::render();
67
68     $total = \Drupal::database()
69       ->query('SELECT COUNT(*) FROM {{ '{' }}{{ entity_type_id }}{{ '}' }}')
70       ->fetchField();
71
72     $build['summary']['#markup'] = $this->t('Total {{ entity_type_label|lower|plural }}: @total', ['@total' => $total]);
73     return $build;
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function buildHeader() {
80     $header['id'] = $this->t('ID');
81 {% if title_base_field %}
82     $header['title'] = $this->t('Title');
83 {% endif %}
84 {% if status_base_field %}
85     $header['status'] = $this->t('Status');
86 {% endif %}
87 {% if author_base_field %}
88     $header['uid'] = $this->t('Author');
89 {% endif %}
90 {% if created_base_field %}
91     $header['created'] = $this->t('Created');
92 {% endif %}
93 {% if changed_base_field %}
94     $header['changed'] = $this->t('Updated');
95 {% endif %}
96     return $header + parent::buildHeader();
97   }
98
99   /**
100    * {@inheritdoc}
101    */
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();
107 {% endif %}
108 {% if status_base_field %}
109     $row['status'] = $entity->isEnabled() ? $this->t('Enabled') : $this->t('Disabled');
110 {% endif %}
111 {% if author_base_field %}
112     $row['uid']['data'] = [
113       '#theme' => 'username',
114       '#account' => $entity->getOwner(),
115     ];
116 {% endif %}
117 {% if created_base_field %}
118     $row['created'] = $this->dateFormatter->format($entity->getCreatedTime());
119 {% endif %}
120 {% if changed_base_field %}
121     $row['changed'] = $this->dateFormatter->format($entity->getChangedTime());
122 {% endif %}
123     return $row + parent::buildRow($entity);
124   }
125
126   /**
127    * {@inheritdoc}
128    */
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;
134     }
135     return $operations;
136   }
137
138 }