Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Entity/EntityListBuilder.php
- 9 core/lib/Drupal/Core/Entity/EntityListBuilder.php
Namespace
Drupal\Core\Entity
File
core/lib/Drupal/Core/Entity/EntityListBuilder.php
View source
<?php
namespace Drupal\Core\Entity;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Url;
use Drupal\Component\Serialization\Json;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
class EntityListBuilder extends EntityHandlerBase implements EntityListBuilderInterface, EntityHandlerInterface {
use MessengerTrait;
use RedirectDestinationTrait;
protected $storage;
protected $entityTypeId;
protected $entityType;
protected $limit = 50;
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager')
->getStorage($entity_type
->id()));
}
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage) {
$this->entityTypeId = $entity_type
->id();
$this->storage = $storage;
$this->entityType = $entity_type;
}
public function getStorage() {
return $this->storage;
}
public function load() {
$entity_ids = $this
->getEntityIds();
return $this->storage
->loadMultiple($entity_ids);
}
protected function getEntityIds() {
return $this
->getEntityListQuery()
->execute();
}
protected function getEntityListQuery() : QueryInterface {
$query = $this
->getStorage()
->getQuery()
->accessCheck(TRUE)
->sort($this->entityType
->getKey('id'));
if ($this->limit) {
$query
->pager($this->limit);
}
return $query;
}
public function getOperations(EntityInterface $entity) {
$operations = $this
->getDefaultOperations($entity);
$operations += $this
->moduleHandler()
->invokeAll('entity_operation', [
$entity,
]);
$this->moduleHandler
->alter('entity_operation', $operations, $entity);
uasort($operations, '\\Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
return $operations;
}
protected function getDefaultOperations(EntityInterface $entity) {
$operations = [];
if ($entity
->access('update') && $entity
->hasLinkTemplate('edit-form')) {
$edit_url = $this
->ensureDestination($entity
->toUrl('edit-form'));
if (!empty($entity
->label())) {
$label = $this
->t('Edit @entity_label', [
'@entity_label' => $entity
->label(),
]);
}
else {
$label = $this
->t('Edit @entity_bundle @entity_id', [
'@entity_bundle' => $entity
->bundle(),
'@entity_id' => $entity
->id(),
]);
}
$attributes = $edit_url
->getOption('attributes') ?: [];
$attributes += [
'aria-label' => $label,
];
$edit_url
->setOption('attributes', $attributes);
$operations['edit'] = [
'title' => $this
->t('Edit'),
'weight' => 10,
'url' => $edit_url,
];
}
if ($entity
->access('delete') && $entity
->hasLinkTemplate('delete-form')) {
$delete_url = $this
->ensureDestination($entity
->toUrl('delete-form'));
if (!empty($entity
->label())) {
$label = $this
->t('Delete @entity_label', [
'@entity_label' => $entity
->label(),
]);
}
else {
$label = $this
->t('Delete @entity_bundle @entity_id', [
'@entity_bundle' => $entity
->bundle(),
'@entity_id' => $entity
->id(),
]);
}
$attributes = $delete_url
->getOption('attributes') ?: [];
$attributes += [
'aria-label' => $label,
];
$delete_url
->setOption('attributes', $attributes);
$operations['delete'] = [
'title' => $this
->t('Delete'),
'weight' => 100,
'attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 880,
]),
],
'url' => $delete_url,
];
}
return $operations;
}
public function buildHeader() {
$row['operations'] = $this
->t('Operations');
return $row;
}
public function buildRow(EntityInterface $entity) {
$row['operations']['data'] = $this
->buildOperations($entity);
return $row;
}
public function buildOperations(EntityInterface $entity) {
$build = [
'#type' => 'operations',
'#links' => $this
->getOperations($entity),
'#attached' => [
'library' => [
'core/drupal.dialog.ajax',
],
],
];
return $build;
}
public function render() {
$build['table'] = [
'#type' => 'table',
'#header' => $this
->buildHeader(),
'#title' => $this
->getTitle(),
'#rows' => [],
'#empty' => $this
->t('There are no @label yet.', [
'@label' => $this->entityType
->getPluralLabel(),
]),
'#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;
}
}
if ($this->limit) {
$build['pager'] = [
'#type' => 'pager',
];
}
return $build;
}
protected function getTitle() {
}
protected function ensureDestination(Url $url) {
return $url
->mergeOptions([
'query' => $this
->getRedirectDestination()
->getAsArray(),
]);
}
}
Classes