View source
<?php
namespace Drupal\field_ui;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Url;
use Drupal\field\FieldConfigInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldConfigListBuilder extends ConfigEntityListBuilder {
use DeprecatedServicePropertyTrait;
protected $deprecatedProperties = [
'entityManager' => 'entity.manager',
];
protected $targetEntityTypeId;
protected $targetBundle;
protected $entityTypeManager;
protected $fieldTypeManager;
protected $entityFieldManager;
public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, FieldTypePluginManagerInterface $field_type_manager, EntityFieldManagerInterface $entity_field_manager = NULL) {
parent::__construct($entity_type, $entity_type_manager
->getStorage($entity_type
->id()));
$this->entityTypeManager = $entity_type_manager;
$this->fieldTypeManager = $field_type_manager;
if (!$entity_field_manager) {
@trigger_error('Calling FieldConfigListBuilder::__construct() with the $entity_field_manager 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_field_manager = \Drupal::service('entity_field.manager');
}
$this->entityFieldManager = $entity_field_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager'), $container
->get('plugin.manager.field.field_type'), $container
->get('entity_field.manager'));
}
public function render($target_entity_type_id = NULL, $target_bundle = NULL) {
$this->targetEntityTypeId = $target_entity_type_id;
$this->targetBundle = $target_bundle;
$build = parent::render();
$build['table']['#attributes']['id'] = 'field-overview';
$build['table']['#empty'] = $this
->t('No fields are present yet.');
return $build;
}
public function load() {
$entities = array_filter($this->entityFieldManager
->getFieldDefinitions($this->targetEntityTypeId, $this->targetBundle), function ($field_definition) {
return $field_definition instanceof FieldConfigInterface;
});
uasort($entities, [
$this->entityType
->getClass(),
'sort',
]);
return $entities;
}
public function buildHeader() {
$header = [
'label' => $this
->t('Label'),
'field_name' => [
'data' => $this
->t('Machine name'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
],
'field_type' => $this
->t('Field type'),
];
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $field_config) {
$field_storage = $field_config
->getFieldStorageDefinition();
$route_parameters = [
'field_config' => $field_config
->id(),
] + FieldUI::getRouteBundleParameter($this->entityTypeManager
->getDefinition($this->targetEntityTypeId), $this->targetBundle);
$row = [
'id' => Html::getClass($field_config
->getName()),
'data' => [
'label' => $field_config
->getLabel(),
'field_name' => $field_config
->getName(),
'field_type' => [
'data' => [
'#type' => 'link',
'#title' => $this->fieldTypeManager
->getDefinitions()[$field_storage
->getType()]['label'],
'#url' => Url::fromRoute("entity.field_config.{$this->targetEntityTypeId}_storage_edit_form", $route_parameters),
'#options' => [
'attributes' => [
'title' => $this
->t('Edit field settings.'),
],
],
],
],
],
];
$row['data'] = $row['data'] + parent::buildRow($field_config);
if ($field_storage
->isLocked()) {
$row['data']['operations'] = [
'data' => [
'#markup' => $this
->t('Locked'),
],
];
$row['class'][] = 'menu-disabled';
}
return $row;
}
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if ($entity
->access('update') && $entity
->hasLinkTemplate("{$entity->getTargetEntityTypeId()}-field-edit-form")) {
$operations['edit'] = [
'title' => $this
->t('Edit'),
'weight' => 10,
'url' => $entity
->toUrl("{$entity->getTargetEntityTypeId()}-field-edit-form"),
'attributes' => [
'title' => $this
->t('Edit field settings.'),
],
];
}
if ($entity
->access('delete') && $entity
->hasLinkTemplate("{$entity->getTargetEntityTypeId()}-field-delete-form")) {
$operations['delete'] = [
'title' => $this
->t('Delete'),
'weight' => 100,
'url' => $entity
->toUrl("{$entity->getTargetEntityTypeId()}-field-delete-form"),
'attributes' => [
'title' => $this
->t('Delete field.'),
],
];
}
$operations['storage-settings'] = [
'title' => $this
->t('Storage settings'),
'weight' => 20,
'attributes' => [
'title' => $this
->t('Edit storage settings.'),
],
'url' => $entity
->toUrl("{$entity->getTargetEntityTypeId()}-storage-edit-form"),
];
return $operations;
}
}