More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / field_ui / src / FieldStorageConfigListBuilder.php
1 <?php
2
3 namespace Drupal\field_ui;
4
5 use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
10 use Drupal\Core\Field\FieldTypePluginManagerInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Defines a class to build a listing of fields.
15  *
16  * @see \Drupal\field\Entity\Field
17  * @see field_ui_entity_info()
18  */
19 class FieldStorageConfigListBuilder extends ConfigEntityListBuilder {
20
21   /**
22    * An array of information about field types.
23    *
24    * @var array
25    */
26   protected $fieldTypes;
27
28   /**
29    * The entity manager.
30    *
31    * @var \Drupal\Core\Entity\EntityManagerInterface
32    */
33   protected $entityManager;
34
35   /**
36    * An array of entity bundle information.
37    *
38    * @var array
39    */
40   protected $bundles;
41
42   /**
43    * The field type manager.
44    *
45    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
46    */
47   protected $fieldTypeManager;
48
49   /**
50    * Constructs a new FieldStorageConfigListBuilder object.
51    *
52    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
53    *   The entity type definition.
54    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
55    *   The entity manager.
56    * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
57    *   The 'field type' plugin manager.
58    */
59   public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, EntityTypeBundleInfoInterface $bundle_info_service) {
60     parent::__construct($entity_type, $entity_manager->getStorage($entity_type->id()));
61
62     $this->entityManager = $entity_manager;
63     $this->bundles = $bundle_info_service->getAllBundleInfo();
64     $this->fieldTypeManager = $field_type_manager;
65     $this->fieldTypes = $this->fieldTypeManager->getDefinitions();
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
72     return new static(
73       $entity_type,
74       $container->get('entity.manager'),
75       $container->get('plugin.manager.field.field_type'),
76       $container->get('entity_type.bundle.info')
77     );
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function buildHeader() {
84     $header['id'] = $this->t('Field name');
85     $header['entity_type'] = $this->t('Entity type');
86     $header['type'] = [
87       'data' => $this->t('Field type'),
88       'class' => [RESPONSIVE_PRIORITY_MEDIUM],
89     ];
90     $header['usage'] = $this->t('Used in');
91     return $header;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function buildRow(EntityInterface $field_storage) {
98     if ($field_storage->isLocked()) {
99       $row['class'] = ['menu-disabled'];
100       $row['data']['id'] = $this->t('@field_name (Locked)', ['@field_name' => $field_storage->getName()]);
101     }
102     else {
103       $row['data']['id'] = $field_storage->getName();
104     }
105
106     $entity_type_id = $field_storage->getTargetEntityTypeId();
107     // Adding the entity type.
108     $row['data']['entity_type'] = $entity_type_id;
109
110     $field_type = $this->fieldTypes[$field_storage->getType()];
111     $row['data']['type'] = $this->t('@type (module: @module)', ['@type' => $field_type['label'], '@module' => $field_type['provider']]);
112
113     $usage = [];
114     foreach ($field_storage->getBundles() as $bundle) {
115       if ($route_info = FieldUI::getOverviewRouteInfo($entity_type_id, $bundle)) {
116         $usage[] = \Drupal::l($this->bundles[$entity_type_id][$bundle]['label'], $route_info);
117       }
118       else {
119         $usage[] = $this->bundles[$entity_type_id][$bundle]['label'];
120       }
121     }
122     $row['data']['usage']['data'] = [
123       '#theme' => 'item_list',
124       '#items' => $usage,
125       '#context' => ['list_style' => 'comma-list'],
126     ];
127     return $row;
128   }
129
130 }