3 namespace Drupal\field;
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;
17 * Controller class for fields.
19 class FieldConfigStorage extends FieldConfigStorageBase {
24 * @var \Drupal\Core\Entity\EntityManagerInterface
26 protected $entityManager;
29 * The state keyvalue collection.
31 * @var \Drupal\Core\State\StateInterface
36 * The field type plugin manager.
38 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
40 protected $fieldTypeManager;
43 * Constructs a FieldConfigStorage object.
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
51 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
52 * The language manager.
53 * @param \Drupal\Core\Entity\EntityManagerInterface $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.
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;
70 public static function createInstance(ContainerInterface $container, EntityTypeInterface $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')
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()) {
92 return parent::importDelete($name, $new_config, $old_config);
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']);
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]);
115 // No specific ID, we need to examine all existing fields.
116 $fields = $this->loadMultiple();
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']]);
129 $fields[$id] = $this->create($config);
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();
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.
144 $checked_value = $field_storage->getName();
148 case 'field_storage_uuid':
149 $checked_value = $field_storage->uuid();
153 $checked_value = $field->uuid();
157 $checked_value = $field->isDeleted();
161 $checked_value = $field->get($key);
165 // Skip to the next field as soon as one condition does not match.
166 if ($checked_value != $value) {
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;
177 return $matching_fields;