comparison core/modules/field/src/FieldConfigStorage.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\Core\Config\Config;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Field\FieldConfigStorageBase;
9 use Drupal\Core\Field\FieldTypePluginManagerInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Drupal\Core\Config\ConfigFactoryInterface;
13 use Drupal\Component\Uuid\UuidInterface;
14 use Drupal\Core\State\StateInterface;
15
16 /**
17 * Controller class for fields.
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 state keyvalue collection.
30 *
31 * @var \Drupal\Core\State\StateInterface
32 */
33 protected $state;
34
35 /**
36 * The field type plugin manager.
37 *
38 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
39 */
40 protected $fieldTypeManager;
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\State\StateInterface $state
56 * The state key value store.
57 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
58 * The field type plugin manager.
59 */
60 public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, EntityManagerInterface $entity_manager, StateInterface $state, FieldTypePluginManagerInterface $field_type_manager) {
61 parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager);
62 $this->entityManager = $entity_manager;
63 $this->state = $state;
64 $this->fieldTypeManager = $field_type_manager;
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('state'),
78 $container->get('plugin.manager.field.field_type')
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 state 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 (stored in state) if needed.
121 if ($include_deleted || !empty($conditions['deleted'])) {
122 $deleted_fields = $this->state->get('field.field.deleted') ?: [];
123 $deleted_storages = $this->state->get('field.storage.deleted') ?: [];
124 foreach ($deleted_fields as $id => $config) {
125 // If the field storage itself is deleted, inject it directly in the field.
126 if (isset($deleted_storages[$config['field_storage_uuid']])) {
127 $config['field_storage'] = $this->entityManager->getStorage('field_storage_config')->create($deleted_storages[$config['field_storage_uuid']]);
128 }
129 $fields[$id] = $this->create($config);
130 }
131 }
132
133 // Collect matching fields.
134 $matching_fields = [];
135 foreach ($fields as $field) {
136 // Some conditions are checked against the field storage.
137 $field_storage = $field->getFieldStorageDefinition();
138
139 // Only keep the field if it matches all conditions.
140 foreach ($conditions as $key => $value) {
141 // Extract the actual value against which the condition is checked.
142 switch ($key) {
143 case 'field_name':
144 $checked_value = $field_storage->getName();
145 break;
146
147 case 'field_id':
148 case 'field_storage_uuid':
149 $checked_value = $field_storage->uuid();
150 break;
151
152 case 'uuid';
153 $checked_value = $field->uuid();
154 break;
155
156 case 'deleted';
157 $checked_value = $field->isDeleted();
158 break;
159
160 default:
161 $checked_value = $field->get($key);
162 break;
163 }
164
165 // Skip to the next field as soon as one condition does not match.
166 if ($checked_value != $value) {
167 continue 2;
168 }
169 }
170
171 // When returning deleted fields, key the results by UUID since they
172 // can include several fields with the same ID.
173 $key = $include_deleted ? $field->uuid() : $field->id();
174 $matching_fields[$key] = $field;
175 }
176
177 return $matching_fields;
178 }
179
180 }