get('entity.manager')->getStorage($entity_type->id()) ); } /** * Constructs a new EntityListBuilder object. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type definition. * @param \Drupal\Core\Entity\EntityStorageInterface $storage * The entity storage class. */ public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage) { $this->entityTypeId = $entity_type->id(); $this->storage = $storage; $this->entityType = $entity_type; } /** * {@inheritdoc} */ public function getStorage() { return $this->storage; } /** * {@inheritdoc} */ public function load() { $entity_ids = $this->getEntityIds(); return $this->storage->loadMultiple($entity_ids); } /** * Loads entity IDs using a pager sorted by the entity id. * * @return array * An array of entity IDs. */ protected function getEntityIds() { $query = $this->getStorage()->getQuery(); $keys = $this->entityType->getKeys(); return $query ->sort($keys['id']) ->pager($this->limit) ->execute(); } /** * Returns the escaped label of an entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity being listed. * * @return string * The escaped entity label. */ protected function getLabel(EntityInterface $entity) { return SafeMarkup::checkPlain($entity->label()); } /** * {@inheritdoc} */ public function getOperations(EntityInterface $entity) { $operations = $this->getDefaultOperations($entity); $operations += $this->moduleHandler()->invokeAll('entity_operation', array($entity)); $this->moduleHandler->alter('entity_operation', $operations, $entity); uasort($operations, '\Drupal\Component\Utility\SortArray::sortByWeightElement'); return $operations; } /** * Gets this list's default operations. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity the operations are for. * * @return array * The array structure is identical to the return value of * self::getOperations(). */ protected function getDefaultOperations(EntityInterface $entity) { $operations = array(); if ($entity->access('update') && $entity->hasLinkTemplate('edit-form')) { $operations['edit'] = array( 'title' => $this->t('Edit'), 'weight' => 10, 'url' => $entity->urlInfo('edit-form'), ); } if ($entity->access('delete') && $entity->hasLinkTemplate('delete-form')) { $operations['delete'] = array( 'title' => $this->t('Delete'), 'weight' => 100, 'url' => $entity->urlInfo('delete-form'), ); } return $operations; } /** * Builds the header row for the entity listing. * * @return array * A render array structure of header strings. * * @see \Drupal\Core\Entity\EntityListBuilder::render() */ public function buildHeader() { $row['operations'] = $this->t('Operations'); return $row; } /** * Builds a row for an entity in the entity listing. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity for this row of the list. * * @return array * A render array structure of fields for this entity. * * @see \Drupal\Core\Entity\EntityListBuilder::render() */ public function buildRow(EntityInterface $entity) { $row['operations']['data'] = $this->buildOperations($entity); return $row; } /** * Builds a renderable list of operation links for the entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity on which the linked operations will be performed. * * @return array * A renderable array of operation links. * * @see \Drupal\Core\Entity\EntityListBuilder::buildRow() */ public function buildOperations(EntityInterface $entity) { $build = array( '#type' => 'operations', '#links' => $this->getOperations($entity), ); return $build; } /** * {@inheritdoc} * * Builds the entity listing as renderable array for table.html.twig. * * @todo Add a link to add a new item to the #empty text. */ public function render() { $build['table'] = array( '#type' => 'table', '#header' => $this->buildHeader(), '#title' => $this->getTitle(), '#rows' => array(), '#empty' => $this->t('There is no @label yet.', array('@label' => $this->entityType->getLabel())), '#cache' => [ 'contexts' => $this->entityType->getListCacheContexts(), ], ); foreach ($this->load() as $entity) { if ($row = $this->buildRow($entity)) { $build['table']['#rows'][$entity->id()] = $row; } } $build['pager'] = array( '#type' => 'pager', ); return $build; } /** * Returns the title of the page. * * @return string * A string title of the page. */ protected function getTitle() { return; } }