c9d89662785b7242698b4186c7f0c1d7a77d64ed
[yaffs-website] / web / core / modules / field_ui / src / EntityDisplayModeListBuilder.php
1 <?php
2
3 namespace Drupal\field_ui;
4
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;
9 use Drupal\Core\Url;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Defines a class to build a listing of view mode entities.
14  *
15  * @see \Drupal\Core\Entity\Entity\EntityViewMode
16  */
17 class EntityDisplayModeListBuilder extends ConfigEntityListBuilder {
18
19   /**
20    * All entity types.
21    *
22    * @var \Drupal\Core\Entity\EntityTypeInterface[]
23    */
24   protected $entityTypes;
25
26   /**
27    * Constructs a new EntityDisplayModeListBuilder object.
28    *
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.
35    */
36   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, array $entity_types) {
37     parent::__construct($entity_type, $storage);
38
39     $this->entityTypes = $entity_types;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
46     $entity_manager = $container->get('entity.manager');
47     return new static(
48       $entity_type,
49       $entity_manager->getStorage($entity_type->id()),
50       $entity_manager->getDefinitions()
51     );
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function buildHeader() {
58     $header['label'] = $this->t('Name');
59     return $header + parent::buildHeader();
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function buildRow(EntityInterface $entity) {
66     $row['label'] = $entity->label();
67     return $row + parent::buildRow($entity);
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function load() {
74     $entities = [];
75     foreach (parent::load() as $entity) {
76       $entities[$entity->getTargetType()][] = $entity;
77     }
78     return $entities;
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function render() {
85     $build = [];
86     foreach ($this->load() as $entity_type => $entities) {
87       if (!isset($this->entityTypes[$entity_type])) {
88         continue;
89       }
90
91       // Filter entities.
92       if (!$this->isValidEntity($entity_type)) {
93         continue;
94       }
95
96       $table = [
97         '#prefix' => '<h2>' . $this->entityTypes[$entity_type]->getLabel() . '</h2>',
98         '#type' => 'table',
99         '#header' => $this->buildHeader(),
100         '#rows' => [],
101       ];
102       foreach ($entities as $entity) {
103         if ($row = $this->buildRow($entity)) {
104           $table['#rows'][$entity->id()] = $row;
105         }
106       }
107
108       // Move content at the top.
109       if ($entity_type == 'node') {
110         $table['#weight'] = -10;
111       }
112
113       $short_type = str_replace(['entity_', '_mode'], '', $this->entityTypeId);
114       $table['#rows']['_add_new'][] = [
115         'data' => [
116           '#type' => 'link',
117           '#url' => Url::fromRoute($short_type == 'view' ? 'entity.entity_view_mode.add_form' : 'entity.entity_form_mode.add_form', ['entity_type_id' => $entity_type]),
118           '#title' => $this->t('Add new %label @entity-type', ['%label' => $this->entityTypes[$entity_type]->getLabel(), '@entity-type' => $this->entityType->getLowercaseLabel()]),
119         ],
120         'colspan' => count($table['#header']),
121       ];
122       $build[$entity_type] = $table;
123     }
124     return $build;
125   }
126
127   /**
128    * Filters entities based on their view builder handlers.
129    *
130    * @param $entity_type
131    *   The entity type of the entity that needs to be validated.
132    *
133    * @return bool
134    *   TRUE if the entity has the correct view builder handler, FALSE if the
135    *   entity doesn't have the correct view builder handler.
136    */
137   protected function isValidEntity($entity_type) {
138     return $this->entityTypes[$entity_type]->get('field_ui_base_route') && $this->entityTypes[$entity_type]->hasViewBuilderClass();
139   }
140
141 }