3 namespace Drupal\field_ui;
5 use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Defines a class to build a listing of view mode entities.
15 * @see \Drupal\Core\Entity\Entity\EntityViewMode
17 class EntityDisplayModeListBuilder extends ConfigEntityListBuilder {
22 * @var \Drupal\Core\Entity\EntityTypeInterface[]
24 protected $entityTypes;
27 * Constructs a new EntityDisplayModeListBuilder object.
29 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
30 * The entity type definition.
31 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
32 * The entity storage class.
33 * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
34 * List of all entity types.
36 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, array $entity_types) {
37 parent::__construct($entity_type, $storage);
39 // Override the default limit (50) in order to display all view modes.
41 $this->entityTypes = $entity_types;
47 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
48 $entity_manager = $container->get('entity.manager');
51 $entity_manager->getStorage($entity_type->id()),
52 $entity_manager->getDefinitions()
59 public function buildHeader() {
60 $header['label'] = $this->t('Name');
61 return $header + parent::buildHeader();
67 public function buildRow(EntityInterface $entity) {
68 $row['label'] = $entity->label();
69 return $row + parent::buildRow($entity);
75 public function load() {
77 foreach (parent::load() as $entity) {
78 $entities[$entity->getTargetType()][] = $entity;
86 public function render() {
88 foreach ($this->load() as $entity_type => $entities) {
89 if (!isset($this->entityTypes[$entity_type])) {
94 if (!$this->isValidEntity($entity_type)) {
99 '#prefix' => '<h2>' . $this->entityTypes[$entity_type]->getLabel() . '</h2>',
101 '#header' => $this->buildHeader(),
104 foreach ($entities as $entity) {
105 if ($row = $this->buildRow($entity)) {
106 $table['#rows'][$entity->id()] = $row;
110 // Move content at the top.
111 if ($entity_type == 'node') {
112 $table['#weight'] = -10;
115 $short_type = str_replace(['entity_', '_mode'], '', $this->entityTypeId);
116 $table['#rows']['_add_new'][] = [
119 '#url' => Url::fromRoute($short_type == 'view' ? 'entity.entity_view_mode.add_form' : 'entity.entity_form_mode.add_form', ['entity_type_id' => $entity_type]),
120 '#title' => $this->t('Add new %label @entity-type', ['%label' => $this->entityTypes[$entity_type]->getLabel(), '@entity-type' => $this->entityType->getLowercaseLabel()]),
122 'colspan' => count($table['#header']),
124 $build[$entity_type] = $table;
130 * Filters entities based on their view builder handlers.
132 * @param $entity_type
133 * The entity type of the entity that needs to be validated.
136 * TRUE if the entity has the correct view builder handler, FALSE if the
137 * entity doesn't have the correct view builder handler.
139 protected function isValidEntity($entity_type) {
140 return $this->entityTypes[$entity_type]->get('field_ui_base_route') && $this->entityTypes[$entity_type]->hasViewBuilderClass();