3 namespace Drupal\Core\Entity;
5 use Symfony\Component\DependencyInjection\ContainerInterface;
8 * Defines a generic implementation to build a listing of entities.
12 class EntityListBuilder extends EntityHandlerBase implements EntityListBuilderInterface, EntityHandlerInterface {
15 * The entity storage class.
17 * @var \Drupal\Core\Entity\EntityStorageInterface
26 protected $entityTypeId;
29 * Information about the entity type.
31 * @var \Drupal\Core\Entity\EntityTypeInterface
33 protected $entityType;
36 * The number of entities to list per page, or FALSE to list all entities.
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).
43 protected $limit = 50;
48 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
51 $container->get('entity.manager')->getStorage($entity_type->id())
56 * Constructs a new EntityListBuilder object.
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.
63 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage) {
64 $this->entityTypeId = $entity_type->id();
65 $this->storage = $storage;
66 $this->entityType = $entity_type;
72 public function getStorage() {
73 return $this->storage;
79 public function load() {
80 $entity_ids = $this->getEntityIds();
81 return $this->storage->loadMultiple($entity_ids);
85 * Loads entity IDs using a pager sorted by the entity id.
88 * An array of entity IDs.
90 protected function getEntityIds() {
91 $query = $this->getStorage()->getQuery()
92 ->sort($this->entityType->getKey('id'));
94 // Only add the pager if a limit is specified.
96 $query->pager($this->limit);
98 return $query->execute();
102 * Gets the label of an entity.
104 * @param \Drupal\Core\Entity\EntityInterface $entity
105 * The entity being listed.
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.
114 protected function getLabel(EntityInterface $entity) {
115 return $entity->label();
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');
131 * Gets this list's default operations.
133 * @param \Drupal\Core\Entity\EntityInterface $entity
134 * The entity the operations are for.
137 * The array structure is identical to the return value of
138 * self::getOperations().
140 protected function getDefaultOperations(EntityInterface $entity) {
142 if ($entity->access('update') && $entity->hasLinkTemplate('edit-form')) {
143 $operations['edit'] = [
144 'title' => $this->t('Edit'),
146 'url' => $entity->urlInfo('edit-form'),
149 if ($entity->access('delete') && $entity->hasLinkTemplate('delete-form')) {
150 $operations['delete'] = [
151 'title' => $this->t('Delete'),
153 'url' => $entity->urlInfo('delete-form'),
161 * Builds the header row for the entity listing.
164 * A render array structure of header strings.
166 * @see \Drupal\Core\Entity\EntityListBuilder::render()
168 public function buildHeader() {
169 $row['operations'] = $this->t('Operations');
174 * Builds a row for an entity in the entity listing.
176 * @param \Drupal\Core\Entity\EntityInterface $entity
177 * The entity for this row of the list.
180 * A render array structure of fields for this entity.
182 * @see \Drupal\Core\Entity\EntityListBuilder::render()
184 public function buildRow(EntityInterface $entity) {
185 $row['operations']['data'] = $this->buildOperations($entity);
190 * Builds a renderable list of operation links for the entity.
192 * @param \Drupal\Core\Entity\EntityInterface $entity
193 * The entity on which the linked operations will be performed.
196 * A renderable array of operation links.
198 * @see \Drupal\Core\Entity\EntityListBuilder::buildRow()
200 public function buildOperations(EntityInterface $entity) {
202 '#type' => 'operations',
203 '#links' => $this->getOperations($entity),
212 * Builds the entity listing as renderable array for table.html.twig.
214 * @todo Add a link to add a new item to the #empty text.
216 public function render() {
219 '#header' => $this->buildHeader(),
220 '#title' => $this->getTitle(),
222 '#empty' => $this->t('There is no @label yet.', ['@label' => $this->entityType->getLabel()]),
224 'contexts' => $this->entityType->getListCacheContexts(),
225 'tags' => $this->entityType->getListCacheTags(),
228 foreach ($this->load() as $entity) {
229 if ($row = $this->buildRow($entity)) {
230 $build['table']['#rows'][$entity->id()] = $row;
234 // Only add the pager if a limit is specified.
244 * Gets the title of the page.
247 * A string title of the page.
249 protected function getTitle() {