3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Messenger\MessengerTrait;
6 use Drupal\Core\Routing\RedirectDestinationTrait;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
11 * Defines a generic implementation to build a listing of entities.
15 class EntityListBuilder extends EntityHandlerBase implements EntityListBuilderInterface, EntityHandlerInterface {
18 use RedirectDestinationTrait;
21 * The entity storage class.
23 * @var \Drupal\Core\Entity\EntityStorageInterface
32 protected $entityTypeId;
35 * Information about the entity type.
37 * @var \Drupal\Core\Entity\EntityTypeInterface
39 protected $entityType;
42 * The number of entities to list per page, or FALSE to list all entities.
44 * For example, set this to FALSE if the list uses client-side filters that
45 * require all entities to be listed (like the views overview).
49 protected $limit = 50;
54 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
57 $container->get('entity.manager')->getStorage($entity_type->id())
62 * Constructs a new EntityListBuilder object.
64 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
65 * The entity type definition.
66 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
67 * The entity storage class.
69 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage) {
70 $this->entityTypeId = $entity_type->id();
71 $this->storage = $storage;
72 $this->entityType = $entity_type;
78 public function getStorage() {
79 return $this->storage;
85 public function load() {
86 $entity_ids = $this->getEntityIds();
87 return $this->storage->loadMultiple($entity_ids);
91 * Loads entity IDs using a pager sorted by the entity id.
94 * An array of entity IDs.
96 protected function getEntityIds() {
97 $query = $this->getStorage()->getQuery()
98 ->sort($this->entityType->getKey('id'));
100 // Only add the pager if a limit is specified.
102 $query->pager($this->limit);
104 return $query->execute();
108 * Gets the label of an entity.
110 * @param \Drupal\Core\Entity\EntityInterface $entity
111 * The entity being listed.
116 * @deprecated in Drupal 8.0.x, will be removed before Drupal 9.0.0
117 * Use $entity->label() instead. This method used to escape the entity
118 * label. The render system's autoescape is now relied upon.
120 protected function getLabel(EntityInterface $entity) {
121 return $entity->label();
127 public function getOperations(EntityInterface $entity) {
128 $operations = $this->getDefaultOperations($entity);
129 $operations += $this->moduleHandler()->invokeAll('entity_operation', [$entity]);
130 $this->moduleHandler->alter('entity_operation', $operations, $entity);
131 uasort($operations, '\Drupal\Component\Utility\SortArray::sortByWeightElement');
137 * Gets this list's default operations.
139 * @param \Drupal\Core\Entity\EntityInterface $entity
140 * The entity the operations are for.
143 * The array structure is identical to the return value of
144 * self::getOperations().
146 protected function getDefaultOperations(EntityInterface $entity) {
148 if ($entity->access('update') && $entity->hasLinkTemplate('edit-form')) {
149 $operations['edit'] = [
150 'title' => $this->t('Edit'),
152 'url' => $this->ensureDestination($entity->toUrl('edit-form')),
155 if ($entity->access('delete') && $entity->hasLinkTemplate('delete-form')) {
156 $operations['delete'] = [
157 'title' => $this->t('Delete'),
159 'url' => $this->ensureDestination($entity->toUrl('delete-form')),
167 * Builds the header row for the entity listing.
170 * A render array structure of header strings.
172 * @see \Drupal\Core\Entity\EntityListBuilder::render()
174 public function buildHeader() {
175 $row['operations'] = $this->t('Operations');
180 * Builds a row for an entity in the entity listing.
182 * @param \Drupal\Core\Entity\EntityInterface $entity
183 * The entity for this row of the list.
186 * A render array structure of fields for this entity.
188 * @see \Drupal\Core\Entity\EntityListBuilder::render()
190 public function buildRow(EntityInterface $entity) {
191 $row['operations']['data'] = $this->buildOperations($entity);
196 * Builds a renderable list of operation links for the entity.
198 * @param \Drupal\Core\Entity\EntityInterface $entity
199 * The entity on which the linked operations will be performed.
202 * A renderable array of operation links.
204 * @see \Drupal\Core\Entity\EntityListBuilder::buildRow()
206 public function buildOperations(EntityInterface $entity) {
208 '#type' => 'operations',
209 '#links' => $this->getOperations($entity),
218 * Builds the entity listing as renderable array for table.html.twig.
220 * @todo Add a link to add a new item to the #empty text.
222 public function render() {
225 '#header' => $this->buildHeader(),
226 '#title' => $this->getTitle(),
228 '#empty' => $this->t('There are no @label yet.', ['@label' => $this->entityType->getPluralLabel()]),
230 'contexts' => $this->entityType->getListCacheContexts(),
231 'tags' => $this->entityType->getListCacheTags(),
234 foreach ($this->load() as $entity) {
235 if ($row = $this->buildRow($entity)) {
236 $build['table']['#rows'][$entity->id()] = $row;
240 // Only add the pager if a limit is specified.
250 * Gets the title of the page.
252 protected function getTitle() {
257 * Ensures that a destination is present on the given URL.
259 * @param \Drupal\Core\Url $url
260 * The URL object to which the destination should be added.
262 * @return \Drupal\Core\Url
263 * The updated URL object.
265 protected function ensureDestination(Url $url) {
266 return $url->mergeOptions(['query' => $this->getRedirectDestination()->getAsArray()]);