EntityListBuilder.php 6.8 KB

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