EntityExtraFieldListBuilder.php in Entity Extra Field 2.0.x
File
src/Controller/EntityExtraFieldListBuilder.php
View source
<?php
namespace Drupal\entity_extra_field\Controller;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityExtraFieldListBuilder extends EntityListBuilder {
protected $entityTypeManager;
protected $currentRouteMatch;
public function __construct(EntityTypeInterface $entity_type, RouteMatchInterface $current_route_match, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_type, $entity_type_manager
->getStorage($entity_type
->id()));
$this->entityTypeManager = $entity_type_manager;
$this->currentRouteMatch = $current_route_match;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('current_route_match'), $container
->get('entity_type.manager'));
}
public function buildHeader() : array {
return [
'label' => $this
->t('Label'),
'field_type' => $this
->t('Field Type'),
'display_type' => $this
->t('Display Type'),
] + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) : array {
return [
'label' => $entity
->label(),
'field_type' => $entity
->getFieldTypeLabel(),
'display_type' => $entity
->getDisplayType(),
] + parent::buildRow($entity);
}
protected function getEntityIds() : array {
$query = $this
->getStorage()
->getQuery();
if ($base_entity_type_id = $this
->getBaseEntityTypeId()) {
$query
->condition('base_entity_type_id', $base_entity_type_id);
}
if ($base_entity_bundle_type = $this
->getBaseEntityBundleType()) {
$query
->condition('base_bundle_type_id', $base_entity_bundle_type
->id());
}
$query
->sort($this->entityType
->getKey('id'));
if ($this->limit) {
$query
->pager($this->limit);
}
return $query
->execute();
}
protected function getBaseEntityTypeId() : ?string {
return $this->currentRouteMatch
->getParameter('entity_type_id');
}
protected function getBaseEntityBundleType() {
$entity_type_id = $this
->getBaseEntityTypeId();
$entity_bundle_type_id = $this->entityTypeManager
->getDefinition($entity_type_id)
->getBundleEntityType();
if (!isset($entity_bundle_type_id)) {
return NULL;
}
return $this->currentRouteMatch
->getParameter($entity_bundle_type_id);
}
}