Version 1
[yaffs-website] / web / core / modules / field_ui / src / FieldConfigListBuilder.php
1 <?php
2
3 namespace Drupal\field_ui;
4
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;
11 use Drupal\Core\Url;
12 use Drupal\field\FieldConfigInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Provides lists of field config entities.
17  */
18 class FieldConfigListBuilder extends ConfigEntityListBuilder {
19
20   /**
21    * The name of the entity type the listed fields are attached to.
22    *
23    * @var string
24    */
25   protected $targetEntityTypeId;
26
27   /**
28    * The name of the bundle the listed fields are attached to.
29    *
30    * @var string
31    */
32   protected $targetBundle;
33
34   /**
35    * The entity manager.
36    *
37    * @var \Drupal\Core\Entity\EntityManagerInterface
38    */
39   protected $entityManager;
40
41   /**
42    * The field type plugin manager.
43    *
44    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
45    */
46   protected $fieldTypeManager;
47
48   /**
49    * Constructs a new class instance.
50    *
51    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
52    *   The entity type definition.
53    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
54    *   The entity manager.
55    * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
56    *   The field type manager
57    */
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()));
60
61     $this->entityManager = $entity_manager;
62     $this->fieldTypeManager = $field_type_manager;
63   }
64
65   /**
66    * {@inheritdoc}
67    */
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'));
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function render($target_entity_type_id = NULL, $target_bundle = NULL) {
76     $this->targetEntityTypeId = $target_entity_type_id;
77     $this->targetBundle = $target_bundle;
78
79     $build = parent::render();
80     $build['table']['#attributes']['id'] = 'field-overview';
81     $build['table']['#empty'] = $this->t('No fields are present yet.');
82
83     return $build;
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function load() {
90     $entities = array_filter($this->entityManager->getFieldDefinitions($this->targetEntityTypeId, $this->targetBundle), function ($field_definition) {
91       return $field_definition instanceof FieldConfigInterface;
92     });
93
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']);
97     return $entities;
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function buildHeader() {
104     $header = [
105       'label' => $this->t('Label'),
106       'field_name' => [
107         'data' => $this->t('Machine name'),
108         'class' => [RESPONSIVE_PRIORITY_MEDIUM],
109       ],
110       'field_type' => $this->t('Field type'),
111     ];
112     return $header + parent::buildHeader();
113   }
114
115   /**
116    * {@inheritdoc}
117    */
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);
124
125     $row = [
126       'id' => Html::getClass($field_config->getName()),
127       'data' => [
128         'label' => $field_config->getLabel(),
129         'field_name' => $field_config->getName(),
130         'field_type' => [
131           'data' => [
132             '#type' => 'link',
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.')]],
136           ],
137         ],
138       ],
139     ];
140
141     // Add the operations.
142     $row['data'] = $row['data'] + parent::buildRow($field_config);
143
144     if ($field_storage->isLocked()) {
145       $row['data']['operations'] = ['data' => ['#markup' => $this->t('Locked')]];
146       $row['class'][] = 'menu-disabled';
147     }
148
149     return $row;
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public function getDefaultOperations(EntityInterface $entity) {
156     /** @var \Drupal\field\FieldConfigInterface $entity */
157     $operations = parent::getDefaultOperations($entity);
158
159     if ($entity->access('update') && $entity->hasLinkTemplate("{$entity->getTargetEntityTypeId()}-field-edit-form")) {
160       $operations['edit'] = [
161         'title' => $this->t('Edit'),
162         'weight' => 10,
163         'url' => $entity->urlInfo("{$entity->getTargetEntityTypeId()}-field-edit-form"),
164         'attributes' => [
165           'title' => $this->t('Edit field settings.')
166         ],
167       ];
168     }
169     if ($entity->access('delete') && $entity->hasLinkTemplate("{$entity->getTargetEntityTypeId()}-field-delete-form")) {
170       $operations['delete'] = [
171         'title' => $this->t('Delete'),
172         'weight' => 100,
173         'url' => $entity->urlInfo("{$entity->getTargetEntityTypeId()}-field-delete-form"),
174         'attributes' => [
175           'title' => $this->t('Delete field.')
176         ],
177       ];
178     }
179
180     $operations['storage-settings'] = [
181       'title' => $this->t('Storage settings'),
182       'weight' => 20,
183       'attributes' => ['title' => $this->t('Edit storage settings.')],
184       'url' => $entity->urlInfo("{$entity->getTargetEntityTypeId()}-storage-edit-form"),
185     ];
186
187     return $operations;
188   }
189
190 }