class EntityListBuilder in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Entity/EntityListBuilder.php \Drupal\Core\Entity\EntityListBuilder
Defines a generic implementation to build a listing of entities.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityListBuilder implements EntityHandlerInterface, EntityListBuilderInterface
Expanded class hierarchy of EntityListBuilder
Related topics
6 files declare their use of EntityListBuilder
- BlockContentListBuilder.php in core/
modules/ block_content/ src/ BlockContentListBuilder.php - Contains \Drupal\block_content\BlockContentListBuilder.
- ConfigEntityListBuilder.php in core/
lib/ Drupal/ Core/ Config/ Entity/ ConfigEntityListBuilder.php - Contains \Drupal\Core\Config\Entity\ConfigEntityListBuilder.
- EntityListBuilderTest.php in core/
tests/ Drupal/ Tests/ Core/ Entity/ EntityListBuilderTest.php - Contains \Drupal\Tests\Core\Entity\EntityListBuilderTest.
- EntityTestListBuilder.php in core/
modules/ system/ tests/ modules/ entity_test/ src/ EntityTestListBuilder.php - Contains \Drupal\entity_test\EntityTestListBuilder.
- NodeListBuilder.php in core/
modules/ node/ src/ NodeListBuilder.php - Contains \Drupal\node\NodeListBuilder.
File
- core/
lib/ Drupal/ Core/ Entity/ EntityListBuilder.php, line 17 - Contains \Drupal\Core\Entity\EntityListBuilder.
Namespace
Drupal\Core\EntityView source
class EntityListBuilder extends EntityHandlerBase implements EntityListBuilderInterface, EntityHandlerInterface {
/**
* The entity storage class.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $storage;
/**
* The entity type ID.
*
* @var string
*/
protected $entityTypeId;
/**
* Information about the entity type.
*
* @var \Drupal\Core\Entity\EntityTypeInterface
*/
protected $entityType;
/**
* The number of entities to list per page, or FALSE to list all entities.
*
* For example, set this to FALSE if the list uses client-side filters that
* require all entities to be listed (like the views overview).
*
* @var int|false
*/
protected $limit = 50;
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->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()
->sort($this->entityType
->getKey('id'));
// Only add the pager if a limit is specified.
if ($this->limit) {
$query
->pager($this->limit);
}
return $query
->execute();
}
/**
* Gets the label of an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being listed.
*
* @return string
* The entity label.
*
* @deprecated in Drupal 8.0.x, will be removed before Drupal 9.0.0
* Use $entity->label() instead. This method used to escape the entity
* label. The render system's autoescape is now relied upon.
*/
protected function getLabel(EntityInterface $entity) {
return $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(),
'tags' => $this->entityType
->getListCacheTags(),
],
);
foreach ($this
->load() as $entity) {
if ($row = $this
->buildRow($entity)) {
$build['table']['#rows'][$entity
->id()] = $row;
}
}
// Only add the pager if a limit is specified.
if ($this->limit) {
$build['pager'] = array(
'#type' => 'pager',
);
}
return $build;
}
/**
* Gets the title of the page.
*
* @return string
* A string title of the page.
*/
protected function getTitle() {
return;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityHandlerBase:: |
protected | property | The module handler to invoke hooks on. | 3 |
EntityHandlerBase:: |
protected | function | Gets the module handler. | 3 |
EntityHandlerBase:: |
public | function | Sets the module handler for this handler. | |
EntityListBuilder:: |
protected | property | Information about the entity type. | |
EntityListBuilder:: |
protected | property | The entity type ID. | |
EntityListBuilder:: |
protected | property | The number of entities to list per page, or FALSE to list all entities. | 2 |
EntityListBuilder:: |
protected | property | The entity storage class. | |
EntityListBuilder:: |
public | function | Builds the header row for the entity listing. | 22 |
EntityListBuilder:: |
public | function | Builds a renderable list of operation links for the entity. | 2 |
EntityListBuilder:: |
public | function | Builds a row for an entity in the entity listing. | 22 |
EntityListBuilder:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface:: |
15 |
EntityListBuilder:: |
protected | function | Gets this list's default operations. | 3 |
EntityListBuilder:: |
protected | function | Loads entity IDs using a pager sorted by the entity id. | 1 |
EntityListBuilder:: |
protected | function | Gets the label of an entity. | |
EntityListBuilder:: |
public | function |
Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface:: |
2 |
EntityListBuilder:: |
public | function |
Gets the entity storage. Overrides EntityListBuilderInterface:: |
|
EntityListBuilder:: |
protected | function | Gets the title of the page. | 1 |
EntityListBuilder:: |
public | function |
Loads entities of this type from storage for listing. Overrides EntityListBuilderInterface:: |
2 |
EntityListBuilder:: |
public | function |
Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilderInterface:: |
11 |
EntityListBuilder:: |
public | function | Constructs a new EntityListBuilder object. | 13 |
StringTranslationTrait:: |
protected | property | The string translation service. | |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |