3 namespace Drupal\field_ui;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityManagerInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Field\FieldTypePluginManagerInterface;
12 use Drupal\field\FieldConfigInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Provides lists of field config entities.
18 class FieldConfigListBuilder extends ConfigEntityListBuilder {
21 * The name of the entity type the listed fields are attached to.
25 protected $targetEntityTypeId;
28 * The name of the bundle the listed fields are attached to.
32 protected $targetBundle;
37 * @var \Drupal\Core\Entity\EntityManagerInterface
39 protected $entityManager;
42 * The field type plugin manager.
44 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
46 protected $fieldTypeManager;
49 * Constructs a new class instance.
51 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
52 * The entity type definition.
53 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
55 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
56 * The field type manager
58 public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager) {
59 parent::__construct($entity_type, $entity_manager->getStorage($entity_type->id()));
61 $this->entityManager = $entity_manager;
62 $this->fieldTypeManager = $field_type_manager;
68 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
69 return new static($entity_type, $container->get('entity.manager'), $container->get('plugin.manager.field.field_type'));
75 public function render($target_entity_type_id = NULL, $target_bundle = NULL) {
76 $this->targetEntityTypeId = $target_entity_type_id;
77 $this->targetBundle = $target_bundle;
79 $build = parent::render();
80 $build['table']['#attributes']['id'] = 'field-overview';
81 $build['table']['#empty'] = $this->t('No fields are present yet.');
89 public function load() {
90 $entities = array_filter($this->entityManager->getFieldDefinitions($this->targetEntityTypeId, $this->targetBundle), function ($field_definition) {
91 return $field_definition instanceof FieldConfigInterface;
94 // Sort the entities using the entity class's sort() method.
95 // See \Drupal\Core\Config\Entity\ConfigEntityBase::sort().
96 uasort($entities, [$this->entityType->getClass(), 'sort']);
103 public function buildHeader() {
105 'label' => $this->t('Label'),
107 'data' => $this->t('Machine name'),
108 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
110 'field_type' => $this->t('Field type'),
112 return $header + parent::buildHeader();
118 public function buildRow(EntityInterface $field_config) {
119 /** @var \Drupal\field\FieldConfigInterface $field_config */
120 $field_storage = $field_config->getFieldStorageDefinition();
121 $route_parameters = [
122 'field_config' => $field_config->id(),
123 ] + FieldUI::getRouteBundleParameter($this->entityManager->getDefinition($this->targetEntityTypeId), $this->targetBundle);
126 'id' => Html::getClass($field_config->getName()),
128 'label' => $field_config->getLabel(),
129 'field_name' => $field_config->getName(),
133 '#title' => $this->fieldTypeManager->getDefinitions()[$field_storage->getType()]['label'],
134 '#url' => Url::fromRoute("entity.field_config.{$this->targetEntityTypeId}_storage_edit_form", $route_parameters),
135 '#options' => ['attributes' => ['title' => $this->t('Edit field settings.')]],
141 // Add the operations.
142 $row['data'] = $row['data'] + parent::buildRow($field_config);
144 if ($field_storage->isLocked()) {
145 $row['data']['operations'] = ['data' => ['#markup' => $this->t('Locked')]];
146 $row['class'][] = 'menu-disabled';
155 public function getDefaultOperations(EntityInterface $entity) {
156 /** @var \Drupal\field\FieldConfigInterface $entity */
157 $operations = parent::getDefaultOperations($entity);
159 if ($entity->access('update') && $entity->hasLinkTemplate("{$entity->getTargetEntityTypeId()}-field-edit-form")) {
160 $operations['edit'] = [
161 'title' => $this->t('Edit'),
163 'url' => $entity->urlInfo("{$entity->getTargetEntityTypeId()}-field-edit-form"),
165 'title' => $this->t('Edit field settings.')
169 if ($entity->access('delete') && $entity->hasLinkTemplate("{$entity->getTargetEntityTypeId()}-field-delete-form")) {
170 $operations['delete'] = [
171 'title' => $this->t('Delete'),
173 'url' => $entity->urlInfo("{$entity->getTargetEntityTypeId()}-field-delete-form"),
175 'title' => $this->t('Delete field.')
180 $operations['storage-settings'] = [
181 'title' => $this->t('Storage settings'),
183 'attributes' => ['title' => $this->t('Edit storage settings.')],
184 'url' => $entity->urlInfo("{$entity->getTargetEntityTypeId()}-storage-edit-form"),