3 namespace Drupal\field;
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\FieldTypePluginManagerInterface;
11 use Drupal\Core\Language\LanguageManagerInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Drupal\Core\Config\ConfigFactoryInterface;
14 use Drupal\Core\Extension\ModuleHandlerInterface;
15 use Drupal\Core\State\StateInterface;
18 * Controller class for "field storage" configuration entities.
20 class FieldStorageConfigStorage extends ConfigEntityStorage {
25 * @var \Drupal\Core\Extension\ModuleHandlerInterface
27 protected $moduleHandler;
32 * @var \Drupal\Core\Entity\EntityManagerInterface
34 protected $entityManager;
37 * The state keyvalue collection.
39 * @var \Drupal\Core\State\StateInterface
44 * The field type plugin manager.
46 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
48 protected $fieldTypeManager;
51 * Constructs a FieldStorageConfigStorage object.
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
59 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
60 * The language manager.
61 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
63 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
65 * @param \Drupal\Core\State\StateInterface $state
66 * The state key value store.
67 * @param \Drupal\Component\Plugin\PluginManagerInterface\FieldTypePluginManagerInterface $field_type_manager
68 * The field type plugin manager.
70 public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, StateInterface $state, FieldTypePluginManagerInterface $field_type_manager) {
71 parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager);
72 $this->entityManager = $entity_manager;
73 $this->moduleHandler = $module_handler;
74 $this->state = $state;
75 $this->fieldTypeManager = $field_type_manager;
81 public static function createInstance(ContainerInterface $container, EntityTypeInterface $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('state'),
90 $container->get('plugin.manager.field.field_type')
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']);
102 /** @var \Drupal\field\FieldStorageConfigInterface[] $storages */
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 state 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]);
115 // No specific ID, we need to examine all existing storages.
116 $storages = $this->loadMultiple();
120 // Merge deleted field storages (living in state) if needed.
121 if ($include_deleted || !empty($conditions['deleted'])) {
122 $deleted_storages = $this->state->get('field.storage.deleted') ?: [];
123 foreach ($deleted_storages as $id => $config) {
124 $storages[$id] = $this->create($config);
128 // Collect matching fields.
130 foreach ($storages as $field) {
131 foreach ($conditions as $key => $value) {
132 // Extract the actual value against which the condition is checked.
133 $checked_value = $field->get($key);
134 // Skip to the next field as soon as one condition does not match.
135 if ($checked_value != $value) {
140 // When returning deleted fields, key the results by UUID since they can
141 // include several fields with the same ID.
142 $key = $include_deleted ? $field->uuid() : $field->id();
143 $matches[$key] = $field;
152 protected function mapFromStorageRecords(array $records) {
153 foreach ($records as $id => &$record) {
154 $class = $this->fieldTypeManager->getPluginClass($record['type']);
156 $config_id = $this->getPrefix() . $id;
157 throw new \RuntimeException("Unable to determine class for field type '{$record['type']}' found in the '$config_id' configuration");
159 $record['settings'] = $class::storageSettingsFromConfigData($record['settings']);
161 return parent::mapFromStorageRecords($records);
167 protected function mapToStorageRecord(EntityInterface $entity) {
168 $record = parent::mapToStorageRecord($entity);
169 $class = $this->fieldTypeManager->getPluginClass($record['type']);
170 $record['settings'] = $class::storageSettingsToConfigData($record['settings']);