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\DeletedFieldsRepositoryInterface;
9 use Drupal\Core\Field\FieldConfigStorageBase;
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\Component\Uuid\UuidInterface;
17 * Storage handler for field config.
19 class FieldConfigStorage extends FieldConfigStorageBase {
24 * @var \Drupal\Core\Entity\EntityManagerInterface
26 protected $entityManager;
29 * The field type plugin manager.
31 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
33 protected $fieldTypeManager;
36 * The deleted fields repository.
38 * @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface
40 protected $deletedFieldsRepository;
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\Field\FieldTypePluginManagerInterface $field_type_manager
56 * The field type plugin manager.
57 * @param \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository
58 * The deleted fields repository.
60 public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, DeletedFieldsRepositoryInterface $deleted_fields_repository) {
61 parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager);
62 $this->entityManager = $entity_manager;
63 $this->fieldTypeManager = $field_type_manager;
64 $this->deletedFieldsRepository = $deleted_fields_repository;
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('plugin.manager.field.field_type'),
78 $container->get('entity_field.deleted_fields_repository')
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 the deleted fields repository 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 from the deleted fields repository if needed.
121 if ($include_deleted || !empty($conditions['deleted'])) {
122 $deleted_field_definitions = $this->deletedFieldsRepository->getFieldDefinitions();
123 foreach ($deleted_field_definitions as $id => $field_definition) {
124 if ($field_definition instanceof FieldConfigInterface) {
125 $fields[$id] = $field_definition;
130 // Collect matching fields.
131 $matching_fields = [];
132 foreach ($fields as $field) {
133 // Some conditions are checked against the field storage.
134 $field_storage = $field->getFieldStorageDefinition();
136 // Only keep the field if it matches all conditions.
137 foreach ($conditions as $key => $value) {
138 // Extract the actual value against which the condition is checked.
141 $checked_value = $field_storage->getName();
145 case 'field_storage_uuid':
146 $checked_value = $field_storage->uuid();
150 $checked_value = $field->uuid();
154 $checked_value = $field->isDeleted();
158 $checked_value = $field->get($key);
162 // Skip to the next field as soon as one condition does not match.
163 if ($checked_value != $value) {
168 // When returning deleted fields, key the results by UUID since they
169 // can include several fields with the same ID.
170 $key = $include_deleted ? $field->uuid() : $field->id();
171 $matching_fields[$key] = $field;
174 return $matching_fields;