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