Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityListBuilder.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 use Symfony\Component\DependencyInjection\ContainerInterface;
6
7 /**
8  * Defines a generic implementation to build a listing of entities.
9  *
10  * @ingroup entity_api
11  */
12 class EntityListBuilder extends EntityHandlerBase implements EntityListBuilderInterface, EntityHandlerInterface {
13
14   /**
15    * The entity storage class.
16    *
17    * @var \Drupal\Core\Entity\EntityStorageInterface
18    */
19   protected $storage;
20
21   /**
22    * The entity type ID.
23    *
24    * @var string
25    */
26   protected $entityTypeId;
27
28   /**
29    * Information about the entity type.
30    *
31    * @var \Drupal\Core\Entity\EntityTypeInterface
32    */
33   protected $entityType;
34
35   /**
36    * The number of entities to list per page, or FALSE to list all entities.
37    *
38    * For example, set this to FALSE if the list uses client-side filters that
39    * require all entities to be listed (like the views overview).
40    *
41    * @var int|false
42    */
43   protected $limit = 50;
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
49     return new static(
50       $entity_type,
51       $container->get('entity.manager')->getStorage($entity_type->id())
52     );
53   }
54
55   /**
56    * Constructs a new EntityListBuilder object.
57    *
58    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
59    *   The entity type definition.
60    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
61    *   The entity storage class.
62    */
63   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage) {
64     $this->entityTypeId = $entity_type->id();
65     $this->storage = $storage;
66     $this->entityType = $entity_type;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getStorage() {
73     return $this->storage;
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function load() {
80     $entity_ids = $this->getEntityIds();
81     return $this->storage->loadMultiple($entity_ids);
82   }
83
84   /**
85    * Loads entity IDs using a pager sorted by the entity id.
86    *
87    * @return array
88    *   An array of entity IDs.
89    */
90   protected function getEntityIds() {
91     $query = $this->getStorage()->getQuery()
92       ->sort($this->entityType->getKey('id'));
93
94     // Only add the pager if a limit is specified.
95     if ($this->limit) {
96       $query->pager($this->limit);
97     }
98     return $query->execute();
99   }
100
101   /**
102    * Gets the label of an entity.
103    *
104    * @param \Drupal\Core\Entity\EntityInterface $entity
105    *   The entity being listed.
106    *
107    * @return string
108    *   The entity label.
109    *
110    * @deprecated in Drupal 8.0.x, will be removed before Drupal 9.0.0
111    *   Use $entity->label() instead. This method used to escape the entity
112    *   label. The render system's autoescape is now relied upon.
113    */
114   protected function getLabel(EntityInterface $entity) {
115     return $entity->label();
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function getOperations(EntityInterface $entity) {
122     $operations = $this->getDefaultOperations($entity);
123     $operations += $this->moduleHandler()->invokeAll('entity_operation', [$entity]);
124     $this->moduleHandler->alter('entity_operation', $operations, $entity);
125     uasort($operations, '\Drupal\Component\Utility\SortArray::sortByWeightElement');
126
127     return $operations;
128   }
129
130   /**
131    * Gets this list's default operations.
132    *
133    * @param \Drupal\Core\Entity\EntityInterface $entity
134    *   The entity the operations are for.
135    *
136    * @return array
137    *   The array structure is identical to the return value of
138    *   self::getOperations().
139    */
140   protected function getDefaultOperations(EntityInterface $entity) {
141     $operations = [];
142     if ($entity->access('update') && $entity->hasLinkTemplate('edit-form')) {
143       $operations['edit'] = [
144         'title' => $this->t('Edit'),
145         'weight' => 10,
146         'url' => $entity->urlInfo('edit-form'),
147       ];
148     }
149     if ($entity->access('delete') && $entity->hasLinkTemplate('delete-form')) {
150       $operations['delete'] = [
151         'title' => $this->t('Delete'),
152         'weight' => 100,
153         'url' => $entity->urlInfo('delete-form'),
154       ];
155     }
156
157     return $operations;
158   }
159
160   /**
161    * Builds the header row for the entity listing.
162    *
163    * @return array
164    *   A render array structure of header strings.
165    *
166    * @see \Drupal\Core\Entity\EntityListBuilder::render()
167    */
168   public function buildHeader() {
169     $row['operations'] = $this->t('Operations');
170     return $row;
171   }
172
173   /**
174    * Builds a row for an entity in the entity listing.
175    *
176    * @param \Drupal\Core\Entity\EntityInterface $entity
177    *   The entity for this row of the list.
178    *
179    * @return array
180    *   A render array structure of fields for this entity.
181    *
182    * @see \Drupal\Core\Entity\EntityListBuilder::render()
183    */
184   public function buildRow(EntityInterface $entity) {
185     $row['operations']['data'] = $this->buildOperations($entity);
186     return $row;
187   }
188
189   /**
190    * Builds a renderable list of operation links for the entity.
191    *
192    * @param \Drupal\Core\Entity\EntityInterface $entity
193    *   The entity on which the linked operations will be performed.
194    *
195    * @return array
196    *   A renderable array of operation links.
197    *
198    * @see \Drupal\Core\Entity\EntityListBuilder::buildRow()
199    */
200   public function buildOperations(EntityInterface $entity) {
201     $build = [
202       '#type' => 'operations',
203       '#links' => $this->getOperations($entity),
204     ];
205
206     return $build;
207   }
208
209   /**
210    * {@inheritdoc}
211    *
212    * Builds the entity listing as renderable array for table.html.twig.
213    *
214    * @todo Add a link to add a new item to the #empty text.
215    */
216   public function render() {
217     $build['table'] = [
218       '#type' => 'table',
219       '#header' => $this->buildHeader(),
220       '#title' => $this->getTitle(),
221       '#rows' => [],
222       '#empty' => $this->t('There is no @label yet.', ['@label' => $this->entityType->getLabel()]),
223       '#cache' => [
224         'contexts' => $this->entityType->getListCacheContexts(),
225         'tags' => $this->entityType->getListCacheTags(),
226       ],
227     ];
228     foreach ($this->load() as $entity) {
229       if ($row = $this->buildRow($entity)) {
230         $build['table']['#rows'][$entity->id()] = $row;
231       }
232     }
233
234     // Only add the pager if a limit is specified.
235     if ($this->limit) {
236       $build['pager'] = [
237         '#type' => 'pager',
238       ];
239     }
240     return $build;
241   }
242
243   /**
244    * Gets the title of the page.
245    *
246    * @return string
247    *   A string title of the page.
248    */
249   protected function getTitle() {
250     return;
251   }
252
253 }