Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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     // Override the default limit (50) in order to display all view modes.
40     $this->limit = FALSE;
41     $this->entityTypes = $entity_types;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
48     $entity_manager = $container->get('entity.manager');
49     return new static(
50       $entity_type,
51       $entity_manager->getStorage($entity_type->id()),
52       $entity_manager->getDefinitions()
53     );
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function buildHeader() {
60     $header['label'] = $this->t('Name');
61     return $header + parent::buildHeader();
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function buildRow(EntityInterface $entity) {
68     $row['label'] = $entity->label();
69     return $row + parent::buildRow($entity);
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function load() {
76     $entities = [];
77     foreach (parent::load() as $entity) {
78       $entities[$entity->getTargetType()][] = $entity;
79     }
80     return $entities;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function render() {
87     $build = [];
88     foreach ($this->load() as $entity_type => $entities) {
89       if (!isset($this->entityTypes[$entity_type])) {
90         continue;
91       }
92
93       // Filter entities.
94       if (!$this->isValidEntity($entity_type)) {
95         continue;
96       }
97
98       $table = [
99         '#prefix' => '<h2>' . $this->entityTypes[$entity_type]->getLabel() . '</h2>',
100         '#type' => 'table',
101         '#header' => $this->buildHeader(),
102         '#rows' => [],
103       ];
104       foreach ($entities as $entity) {
105         if ($row = $this->buildRow($entity)) {
106           $table['#rows'][$entity->id()] = $row;
107         }
108       }
109
110       // Move content at the top.
111       if ($entity_type == 'node') {
112         $table['#weight'] = -10;
113       }
114
115       $short_type = str_replace(['entity_', '_mode'], '', $this->entityTypeId);
116       $table['#rows']['_add_new'][] = [
117         'data' => [
118           '#type' => 'link',
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()]),
121         ],
122         'colspan' => count($table['#header']),
123       ];
124       $build[$entity_type] = $table;
125     }
126     return $build;
127   }
128
129   /**
130    * Filters entities based on their view builder handlers.
131    *
132    * @param $entity_type
133    *   The entity type of the entity that needs to be validated.
134    *
135    * @return bool
136    *   TRUE if the entity has the correct view builder handler, FALSE if the
137    *   entity doesn't have the correct view builder handler.
138    */
139   protected function isValidEntity($entity_type) {
140     return $this->entityTypes[$entity_type]->get('field_ui_base_route') && $this->entityTypes[$entity_type]->hasViewBuilderClass();
141   }
142
143 }