Backup of db before drupal security update
[yaffs-website] / web / core / modules / config_translation / src / Controller / ConfigTranslationFieldListBuilder.php
1 <?php
2
3 namespace Drupal\config_translation\Controller;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Defines the config translation list builder for field entities.
14  */
15 class ConfigTranslationFieldListBuilder extends ConfigTranslationEntityListBuilder {
16
17   /**
18    * The name of the entity type the fields are attached to.
19    *
20    * @var string
21    */
22   protected $baseEntityType = '';
23
24   /**
25    * An array containing the base entity type's definition.
26    *
27    * @var array
28    */
29   protected $baseEntityInfo = [];
30
31   /**
32    * The bundle info for the base entity type.
33    *
34    * @var array
35    */
36   protected $baseEntityBundles = [];
37
38   /**
39    * The entity manager.
40    *
41    * @var \Drupal\Core\Entity\EntityManagerInterface
42    */
43   protected $entityManager;
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
49     $entity_manager = $container->get('entity.manager');
50     return new static(
51       $entity_type,
52       $entity_manager->getStorage($entity_type->id()),
53       $entity_manager
54     );
55   }
56
57   /**
58    * Constructs a new ConfigTranslationFieldListBuilder object.
59    *
60    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
61    *   The entity type definition.
62    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
63    *   The entity storage class.
64    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
65    *   The entity manager.
66    */
67   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityManagerInterface $entity_manager) {
68     parent::__construct($entity_type, $storage);
69     $this->entityManager = $entity_manager;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function setMapperDefinition($mapper_definition) {
76     $this->baseEntityType = $mapper_definition['base_entity_type'];
77     $this->baseEntityInfo = $this->entityManager->getDefinition($this->baseEntityType);
78     $this->baseEntityBundles = $this->entityManager->getBundleInfo($this->baseEntityType);
79     return $this;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function load() {
86     // It is not possible to use the standard load method, because this needs
87     // all field entities only for the given baseEntityType.
88     $ids = \Drupal::entityQuery('field_config')
89       ->condition('id', $this->baseEntityType . '.', 'STARTS_WITH')
90       ->execute();
91     return $this->storage->loadMultiple($ids);
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function getFilterLabels() {
98     $info = parent::getFilterLabels();
99     $bundle = $this->baseEntityInfo->getBundleLabel() ?: $this->t('Bundle');
100     $bundle = Unicode::strtolower($bundle);
101
102     $info['placeholder'] = $this->t('Enter field or @bundle', ['@bundle' => $bundle]);
103     $info['description'] = $this->t('Enter a part of the field or @bundle to filter by.', ['@bundle' => $bundle]);
104
105     return $info;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function buildRow(EntityInterface $entity) {
112     $row['label'] = [
113       'data' => $entity->label(),
114       'class' => 'table-filter-text-source',
115     ];
116
117     if ($this->displayBundle()) {
118       $bundle = $entity->get('bundle');
119       $row['bundle'] = [
120         'data' => $this->baseEntityBundles[$bundle]['label'],
121         'class' => 'table-filter-text-source',
122       ];
123     }
124
125     return $row + parent::buildRow($entity);
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function buildHeader() {
132     $header['label'] = $this->t('Field');
133     if ($this->displayBundle()) {
134       $header['bundle'] = $this->baseEntityInfo->getBundleLabel() ?: $this->t('Bundle');
135     }
136     return $header + parent::buildHeader();
137   }
138
139   /**
140    * Controls the visibility of the bundle column on field list pages.
141    *
142    * @return bool
143    *   Whenever the bundle is displayed or not.
144    */
145   public function displayBundle() {
146     // The bundle key is explicitly defined in the entity definition.
147     if ($this->baseEntityInfo->getKey('bundle')) {
148       return TRUE;
149     }
150
151     // There is more than one bundle defined.
152     if (count($this->baseEntityBundles) > 1) {
153       return TRUE;
154     }
155
156     // The defined bundle ones not match the entity type name.
157     if (!empty($this->baseEntityBundles) && !isset($this->baseEntityBundles[$this->baseEntityType])) {
158       return TRUE;
159     }
160
161     return FALSE;
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   public function sortRows($a, $b) {
168     return $this->sortRowsMultiple($a, $b, ['bundle', 'label']);
169   }
170
171 }