a0e460f00f9c74d82c13c37a39f12ee1b9e6e46b
[yaffs-website] / web / core / modules / field / src / FieldConfigStorage.php
1 <?php
2
3 namespace Drupal\field;
4
5 use Drupal\Core\Config\Config;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Field\DeletedFieldsRepositoryInterface;
9 use Drupal\Core\Field\FieldConfigStorageBase;
10 use Drupal\Core\Field\FieldTypePluginManagerInterface;
11 use Drupal\Core\Language\LanguageManagerInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Drupal\Core\Config\ConfigFactoryInterface;
14 use Drupal\Component\Uuid\UuidInterface;
15
16 /**
17  * Storage handler for field config.
18  */
19 class FieldConfigStorage extends FieldConfigStorageBase {
20
21   /**
22    * The entity manager.
23    *
24    * @var \Drupal\Core\Entity\EntityManagerInterface
25    */
26   protected $entityManager;
27
28   /**
29    * The field type plugin manager.
30    *
31    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
32    */
33   protected $fieldTypeManager;
34
35   /**
36    * The deleted fields repository.
37    *
38    * @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface
39    */
40   protected $deletedFieldsRepository;
41
42   /**
43    * Constructs a FieldConfigStorage object.
44    *
45    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
46    *   The entity type definition.
47    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
48    *   The config factory service.
49    * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
50    *   The UUID service.
51    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
52    *   The language manager.
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 plugin manager.
57    * @param \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository
58    *   The deleted fields repository.
59    */
60   public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, DeletedFieldsRepositoryInterface $deleted_fields_repository) {
61     parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager);
62     $this->entityManager = $entity_manager;
63     $this->fieldTypeManager = $field_type_manager;
64     $this->deletedFieldsRepository = $deleted_fields_repository;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
71     return new static(
72       $entity_type,
73       $container->get('config.factory'),
74       $container->get('uuid'),
75       $container->get('language_manager'),
76       $container->get('entity.manager'),
77       $container->get('plugin.manager.field.field_type'),
78       $container->get('entity_field.deleted_fields_repository')
79     );
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function importDelete($name, Config $new_config, Config $old_config) {
86     // If the field storage has been deleted in the same import, the field will
87     // be deleted by then, and there is nothing left to do. Just return TRUE so
88     // that the file does not get written to active store.
89     if (!$old_config->get()) {
90       return TRUE;
91     }
92     return parent::importDelete($name, $new_config, $old_config);
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function loadByProperties(array $conditions = []) {
99     // Include deleted fields if specified in the $conditions parameters.
100     $include_deleted = isset($conditions['include_deleted']) ? $conditions['include_deleted'] : FALSE;
101     unset($conditions['include_deleted']);
102
103     $fields = [];
104
105     // Get fields stored in configuration. If we are explicitly looking for
106     // deleted fields only, this can be skipped, because they will be
107     // retrieved from the deleted fields repository below.
108     if (empty($conditions['deleted'])) {
109       if (isset($conditions['entity_type']) && isset($conditions['bundle']) && isset($conditions['field_name'])) {
110         // Optimize for the most frequent case where we do have a specific ID.
111         $id = $conditions['entity_type'] . '.' . $conditions['bundle'] . '.' . $conditions['field_name'];
112         $fields = $this->loadMultiple([$id]);
113       }
114       else {
115         // No specific ID, we need to examine all existing fields.
116         $fields = $this->loadMultiple();
117       }
118     }
119
120     // Merge deleted fields from the deleted fields repository if needed.
121     if ($include_deleted || !empty($conditions['deleted'])) {
122       $deleted_field_definitions = $this->deletedFieldsRepository->getFieldDefinitions();
123       foreach ($deleted_field_definitions as $id => $field_definition) {
124         if ($field_definition instanceof FieldConfigInterface) {
125           $fields[$id] = $field_definition;
126         }
127       }
128     }
129
130     // Collect matching fields.
131     $matching_fields = [];
132     foreach ($fields as $field) {
133       // Some conditions are checked against the field storage.
134       $field_storage = $field->getFieldStorageDefinition();
135
136       // Only keep the field if it matches all conditions.
137       foreach ($conditions as $key => $value) {
138         // Extract the actual value against which the condition is checked.
139         switch ($key) {
140           case 'field_name':
141             $checked_value = $field_storage->getName();
142             break;
143
144           case 'field_id':
145           case 'field_storage_uuid':
146             $checked_value = $field_storage->uuid();
147             break;
148
149           case 'uuid';
150             $checked_value = $field->uuid();
151             break;
152
153           case 'deleted';
154             $checked_value = $field->isDeleted();
155             break;
156
157           default:
158             $checked_value = $field->get($key);
159             break;
160         }
161
162         // Skip to the next field as soon as one condition does not match.
163         if ($checked_value != $value) {
164           continue 2;
165         }
166       }
167
168       // When returning deleted fields, key the results by UUID since they
169       // can include several fields with the same ID.
170       $key = $include_deleted ? $field->uuid() : $field->id();
171       $matching_fields[$key] = $field;
172     }
173
174     return $matching_fields;
175   }
176
177 }