3 namespace Drupal\config_translation\Controller;
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;
13 * Defines the config translation list builder for field entities.
15 class ConfigTranslationFieldListBuilder extends ConfigTranslationEntityListBuilder {
18 * The name of the entity type the fields are attached to.
22 protected $baseEntityType = '';
25 * An array containing the base entity type's definition.
29 protected $baseEntityInfo = [];
32 * The bundle info for the base entity type.
36 protected $baseEntityBundles = [];
41 * @var \Drupal\Core\Entity\EntityManagerInterface
43 protected $entityManager;
48 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
49 $entity_manager = $container->get('entity.manager');
52 $entity_manager->getStorage($entity_type->id()),
58 * Constructs a new ConfigTranslationFieldListBuilder object.
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
67 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityManagerInterface $entity_manager) {
68 parent::__construct($entity_type, $storage);
69 $this->entityManager = $entity_manager;
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);
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')
91 return $this->storage->loadMultiple($ids);
97 public function getFilterLabels() {
98 $info = parent::getFilterLabels();
99 $bundle = $this->baseEntityInfo->getBundleLabel() ?: $this->t('Bundle');
100 $bundle = Unicode::strtolower($bundle);
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]);
111 public function buildRow(EntityInterface $entity) {
113 'data' => $entity->label(),
114 'class' => 'table-filter-text-source',
117 if ($this->displayBundle()) {
118 $bundle = $entity->get('bundle');
120 'data' => $this->baseEntityBundles[$bundle]['label'],
121 'class' => 'table-filter-text-source',
125 return $row + parent::buildRow($entity);
131 public function buildHeader() {
132 $header['label'] = $this->t('Field');
133 if ($this->displayBundle()) {
134 $header['bundle'] = $this->baseEntityInfo->getBundleLabel() ?: $this->t('Bundle');
136 return $header + parent::buildHeader();
140 * Controls the visibility of the bundle column on field list pages.
143 * Whenever the bundle is displayed or not.
145 public function displayBundle() {
146 // The bundle key is explicitly defined in the entity definition.
147 if ($this->baseEntityInfo->getKey('bundle')) {
151 // There is more than one bundle defined.
152 if (count($this->baseEntityBundles) > 1) {
156 // The defined bundle ones not match the entity type name.
157 if (!empty($this->baseEntityBundles) && !isset($this->baseEntityBundles[$this->baseEntityType])) {
167 public function sortRows($a, $b) {
168 return $this->sortRowsMultiple($a, $b, ['bundle', 'label']);