comparison core/modules/field/src/FieldStorageConfigStorage.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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\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;
16
17 /**
18 * Controller class 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 state keyvalue collection.
38 *
39 * @var \Drupal\Core\State\StateInterface
40 */
41 protected $state;
42
43 /**
44 * The field type plugin manager.
45 *
46 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
47 */
48 protected $fieldTypeManager;
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\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.
69 */
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;
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('state'),
90 $container->get('plugin.manager.field.field_type')
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 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]);
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 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);
125 }
126 }
127
128 // Collect matching fields.
129 $matches = [];
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) {
136 continue 2;
137 }
138 }
139
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;
144 }
145
146 return $matches;
147 }
148
149 /**
150 * {@inheritdoc}
151 */
152 protected function mapFromStorageRecords(array $records) {
153 foreach ($records as $id => &$record) {
154 $class = $this->fieldTypeManager->getPluginClass($record['type']);
155 if (empty($class)) {
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");
158 }
159 $record['settings'] = $class::storageSettingsFromConfigData($record['settings']);
160 }
161 return parent::mapFromStorageRecords($records);
162 }
163
164 /**
165 * {@inheritdoc}
166 */
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']);
171 return $record;
172 }
173
174 }