ConfigTranslationFieldListBuilder.php in Zircon Profile 8
File
core/modules/config_translation/src/Controller/ConfigTranslationFieldListBuilder.php
View source
<?php
namespace Drupal\config_translation\Controller;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConfigTranslationFieldListBuilder extends ConfigTranslationEntityListBuilder {
protected $baseEntityType = '';
protected $baseEntityInfo = array();
protected $baseEntityBundles = array();
protected $entityManager;
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
$entity_manager = $container
->get('entity.manager');
return new static($entity_type, $entity_manager
->getStorage($entity_type
->id()), $entity_manager);
}
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityManagerInterface $entity_manager) {
parent::__construct($entity_type, $storage);
$this->entityManager = $entity_manager;
}
public function setMapperDefinition($mapper_definition) {
$this->baseEntityType = $mapper_definition['base_entity_type'];
$this->baseEntityInfo = $this->entityManager
->getDefinition($this->baseEntityType);
$this->baseEntityBundles = $this->entityManager
->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 = Unicode::strtolower($bundle);
$info['placeholder'] = $this
->t('Enter field or @bundle', array(
'@bundle' => $bundle,
));
$info['description'] = $this
->t('Enter a part of the field or @bundle to filter by.', array(
'@bundle' => $bundle,
));
return $info;
}
public function buildRow(EntityInterface $entity) {
$row['label'] = array(
'data' => $entity
->label(),
'class' => 'table-filter-text-source',
);
if ($this
->displayBundle()) {
$bundle = $entity
->get('bundle');
$row['bundle'] = array(
'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, array(
'bundle',
'label',
));
}
}