3 namespace Drupal\config_translation\Controller;
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;
12 * Defines the config translation list builder for field entities.
14 class ConfigTranslationFieldListBuilder extends ConfigTranslationEntityListBuilder {
17 * The name of the entity type the fields are attached to.
21 protected $baseEntityType = '';
24 * An array containing the base entity type's definition.
28 protected $baseEntityInfo = [];
31 * The bundle info for the base entity type.
35 protected $baseEntityBundles = [];
40 * @var \Drupal\Core\Entity\EntityManagerInterface
42 protected $entityManager;
47 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
48 $entity_manager = $container->get('entity.manager');
51 $entity_manager->getStorage($entity_type->id()),
57 * Constructs a new ConfigTranslationFieldListBuilder object.
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
66 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityManagerInterface $entity_manager) {
67 parent::__construct($entity_type, $storage);
68 $this->entityManager = $entity_manager;
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);
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')
90 return $this->storage->loadMultiple($ids);
96 public function getFilterLabels() {
97 $info = parent::getFilterLabels();
98 $bundle = $this->baseEntityInfo->getBundleLabel() ?: $this->t('Bundle');
99 $bundle = mb_strtolower($bundle);
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]);
110 public function buildRow(EntityInterface $entity) {
112 'data' => $entity->label(),
113 'class' => 'table-filter-text-source',
116 if ($this->displayBundle()) {
117 $bundle = $entity->get('bundle');
119 'data' => $this->baseEntityBundles[$bundle]['label'],
120 'class' => 'table-filter-text-source',
124 return $row + parent::buildRow($entity);
130 public function buildHeader() {
131 $header['label'] = $this->t('Field');
132 if ($this->displayBundle()) {
133 $header['bundle'] = $this->baseEntityInfo->getBundleLabel() ?: $this->t('Bundle');
135 return $header + parent::buildHeader();
139 * Controls the visibility of the bundle column on field list pages.
142 * Whenever the bundle is displayed or not.
144 public function displayBundle() {
145 // The bundle key is explicitly defined in the entity definition.
146 if ($this->baseEntityInfo->getKey('bundle')) {
150 // There is more than one bundle defined.
151 if (count($this->baseEntityBundles) > 1) {
155 // The defined bundle ones not match the entity type name.
156 if (!empty($this->baseEntityBundles) && !isset($this->baseEntityBundles[$this->baseEntityType])) {
166 public function sortRows($a, $b) {
167 return $this->sortRowsMultiple($a, $b, ['bundle', 'label']);