More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / field / src / FieldStorageConfigStorage.php
1 <?php
2
3 namespace Drupal\field;
4
5 use Drupal\Component\Uuid\UuidInterface;
6 use Drupal\Core\Config\Entity\ConfigEntityStorage;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityManagerInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Field\DeletedFieldsRepositoryInterface;
11 use Drupal\Core\Field\FieldTypePluginManagerInterface;
12 use Drupal\Core\Language\LanguageManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Drupal\Core\Config\ConfigFactoryInterface;
15 use Drupal\Core\Extension\ModuleHandlerInterface;
16
17 /**
18  * Storage handler for "field storage" configuration entities.
19  */
20 class FieldStorageConfigStorage extends ConfigEntityStorage {
21
22   /**
23    * The module handler.
24    *
25    * @var \Drupal\Core\Extension\ModuleHandlerInterface
26    */
27   protected $moduleHandler;
28
29   /**
30    * The entity manager.
31    *
32    * @var \Drupal\Core\Entity\EntityManagerInterface
33    */
34   protected $entityManager;
35
36   /**
37    * The field type plugin manager.
38    *
39    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
40    */
41   protected $fieldTypeManager;
42
43   /**
44    * The deleted fields repository.
45    *
46    * @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface
47    */
48   protected $deletedFieldsRepository;
49
50   /**
51    * Constructs a FieldStorageConfigStorage object.
52    *
53    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
54    *   The entity type definition.
55    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
56    *   The config factory service.
57    * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
58    *   The UUID service.
59    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
60    *   The language manager.
61    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
62    *   The entity manager.
63    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
64    *   The module handler.
65    * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
66    *   The field type plugin manager.
67    * @param \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository
68    *   The deleted fields repository.
69    */
70   public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager, DeletedFieldsRepositoryInterface $deleted_fields_repository) {
71     parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager);
72     $this->entityManager = $entity_manager;
73     $this->moduleHandler = $module_handler;
74     $this->fieldTypeManager = $field_type_manager;
75     $this->deletedFieldsRepository = $deleted_fields_repository;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
82     return new static(
83       $entity_type,
84       $container->get('config.factory'),
85       $container->get('uuid'),
86       $container->get('language_manager'),
87       $container->get('entity.manager'),
88       $container->get('module_handler'),
89       $container->get('plugin.manager.field.field_type'),
90       $container->get('entity_field.deleted_fields_repository')
91     );
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function loadByProperties(array $conditions = []) {
98     // Include deleted fields if specified in the $conditions parameters.
99     $include_deleted = isset($conditions['include_deleted']) ? $conditions['include_deleted'] : FALSE;
100     unset($conditions['include_deleted']);
101
102     /** @var \Drupal\field\FieldStorageConfigInterface[] $storages */
103     $storages = [];
104
105     // Get field storages living in configuration. If we are explicitly looking
106     // for deleted storages 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['field_name'])) {
110         // Optimize for the most frequent case where we do have a specific ID.
111         $id = $conditions['entity_type'] . $conditions['field_name'];
112         $storages = $this->loadMultiple([$id]);
113       }
114       else {
115         // No specific ID, we need to examine all existing storages.
116         $storages = $this->loadMultiple();
117       }
118     }
119
120     // Merge deleted field storage definitions from the deleted fields
121     // repository if needed.
122     if ($include_deleted || !empty($conditions['deleted'])) {
123       $deleted_storage_definitions = $this->deletedFieldsRepository->getFieldStorageDefinitions();
124       foreach ($deleted_storage_definitions as $id => $field_storage_definition) {
125         if ($field_storage_definition instanceof FieldStorageConfigInterface) {
126           $storages[$id] = $field_storage_definition;
127         }
128       }
129     }
130
131     // Collect matching fields.
132     $matches = [];
133     foreach ($storages as $field) {
134       foreach ($conditions as $key => $value) {
135         // Extract the actual value against which the condition is checked.
136         $checked_value = $field->get($key);
137         // Skip to the next field as soon as one condition does not match.
138         if ($checked_value != $value) {
139           continue 2;
140         }
141       }
142
143       // When returning deleted fields, key the results by UUID since they can
144       // include several fields with the same ID.
145       $key = $include_deleted ? $field->uuid() : $field->id();
146       $matches[$key] = $field;
147     }
148
149     return $matches;
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   protected function mapFromStorageRecords(array $records) {
156     foreach ($records as $id => &$record) {
157       $class = $this->fieldTypeManager->getPluginClass($record['type']);
158       if (empty($class)) {
159         $config_id = $this->getPrefix() . $id;
160         throw new \RuntimeException("Unable to determine class for field type '{$record['type']}' found in the '$config_id' configuration");
161       }
162       $record['settings'] = $class::storageSettingsFromConfigData($record['settings']);
163     }
164     return parent::mapFromStorageRecords($records);
165   }
166
167   /**
168    * {@inheritdoc}
169    */
170   protected function mapToStorageRecord(EntityInterface $entity) {
171     $record = parent::mapToStorageRecord($entity);
172     $class = $this->fieldTypeManager->getPluginClass($record['type']);
173     $record['settings'] = $class::storageSettingsToConfigData($record['settings']);
174     return $record;
175   }
176
177 }