View source
<?php
namespace Drupal\config_translation\Controller;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConfigTranslationFieldListBuilder extends ConfigTranslationEntityListBuilder {
use DeprecatedServicePropertyTrait;
protected $deprecatedProperties = [
'entityManager' => 'entity.manager',
];
protected $baseEntityType = '';
protected $baseEntityInfo;
protected $baseEntityBundles = [];
protected $entityTypeManager;
protected $entityTypeBundleInfo;
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
$entity_type_manager = $container
->get('entity_type.manager');
$entity_type_bundle_info = $container
->get('entity_type.bundle.info');
return new static($entity_type, $entity_type_manager
->getStorage($entity_type
->id()), $entity_type_manager, $entity_type_bundle_info);
}
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL) {
parent::__construct($entity_type, $storage);
$this->entityTypeManager = $entity_type_manager;
if (!$entity_type_bundle_info) {
@trigger_error('Calling ConfigTranslationFieldListBuilder::__construct() with the $entity_type_bundle_info argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
}
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
public function setMapperDefinition($mapper_definition) {
$this->baseEntityType = $mapper_definition['base_entity_type'];
$this->baseEntityInfo = $this->entityTypeManager
->getDefinition($this->baseEntityType);
$this->baseEntityBundles = $this->entityTypeBundleInfo
->getBundleInfo($this->baseEntityType);
return $this;
}
public function load() {
$ids = \Drupal::entityQuery('field_config')
->condition('id', $this->baseEntityType . '.', 'STARTS_WITH')
->execute();
return $this->storage
->loadMultiple($ids);
}
public function getFilterLabels() {
$info = parent::getFilterLabels();
$bundle = $this->baseEntityInfo
->getBundleLabel() ?: $this
->t('Bundle');
$bundle = mb_strtolower($bundle);
$info['placeholder'] = $this
->t('Enter field or @bundle', [
'@bundle' => $bundle,
]);
$info['description'] = $this
->t('Enter a part of the field or @bundle to filter by.', [
'@bundle' => $bundle,
]);
return $info;
}
public function buildRow(EntityInterface $entity) {
$row['label'] = [
'data' => $entity
->label(),
'class' => 'table-filter-text-source',
];
if ($this
->displayBundle()) {
$bundle = $entity
->get('bundle');
$row['bundle'] = [
'data' => $this->baseEntityBundles[$bundle]['label'],
'class' => 'table-filter-text-source',
];
}
return $row + parent::buildRow($entity);
}
public function buildHeader() {
$header['label'] = $this
->t('Field');
if ($this
->displayBundle()) {
$header['bundle'] = $this->baseEntityInfo
->getBundleLabel() ?: $this
->t('Bundle');
}
return $header + parent::buildHeader();
}
public function displayBundle() {
if ($this->baseEntityInfo
->getKey('bundle')) {
return TRUE;
}
if (count($this->baseEntityBundles) > 1) {
return TRUE;
}
if (!empty($this->baseEntityBundles) && !isset($this->baseEntityBundles[$this->baseEntityType])) {
return TRUE;
}
return FALSE;
}
public function sortRows($a, $b) {
return $this
->sortRowsMultiple($a, $b, [
'bundle',
'label',
]);
}
}