annotate core/modules/field/src/FieldConfigStorage.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\field;
Chris@0 4
Chris@17 5 use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
Chris@0 6 use Drupal\Core\Config\Config;
Chris@0 7 use Drupal\Core\Entity\EntityManagerInterface;
Chris@0 8 use Drupal\Core\Entity\EntityTypeInterface;
Chris@14 9 use Drupal\Core\Field\DeletedFieldsRepositoryInterface;
Chris@0 10 use Drupal\Core\Field\FieldConfigStorageBase;
Chris@0 11 use Drupal\Core\Field\FieldTypePluginManagerInterface;
Chris@0 12 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 13 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 14 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 15 use Drupal\Component\Uuid\UuidInterface;
Chris@0 16
Chris@0 17 /**
Chris@14 18 * Storage handler for field config.
Chris@0 19 */
Chris@0 20 class FieldConfigStorage extends FieldConfigStorageBase {
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The entity manager.
Chris@0 24 *
Chris@0 25 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 26 */
Chris@0 27 protected $entityManager;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * The field type plugin manager.
Chris@0 31 *
Chris@0 32 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
Chris@0 33 */
Chris@0 34 protected $fieldTypeManager;
Chris@0 35
Chris@0 36 /**
Chris@14 37 * The deleted fields repository.
Chris@14 38 *
Chris@14 39 * @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface
Chris@14 40 */
Chris@14 41 protected $deletedFieldsRepository;
Chris@14 42
Chris@14 43 /**
Chris@0 44 * Constructs a FieldConfigStorage object.
Chris@0 45 *
Chris@0 46 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 47 * The entity type definition.
Chris@0 48 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 49 * The config factory service.
Chris@0 50 * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
Chris@0 51 * The UUID service.
Chris@0 52 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@0 53 * The language manager.
Chris@0 54 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 55 * The entity manager.
Chris@0 56 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
Chris@0 57 * The field type plugin manager.
Chris@14 58 * @param \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository
Chris@14 59 * The deleted fields repository.
Chris@17 60 * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache
Chris@17 61 * The memory cache.
Chris@0 62 */
Chris@17 63 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, MemoryCacheInterface $memory_cache) {
Chris@17 64 parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager, $memory_cache);
Chris@0 65 $this->entityManager = $entity_manager;
Chris@0 66 $this->fieldTypeManager = $field_type_manager;
Chris@14 67 $this->deletedFieldsRepository = $deleted_fields_repository;
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * {@inheritdoc}
Chris@0 72 */
Chris@0 73 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
Chris@0 74 return new static(
Chris@0 75 $entity_type,
Chris@0 76 $container->get('config.factory'),
Chris@0 77 $container->get('uuid'),
Chris@0 78 $container->get('language_manager'),
Chris@0 79 $container->get('entity.manager'),
Chris@14 80 $container->get('plugin.manager.field.field_type'),
Chris@17 81 $container->get('entity_field.deleted_fields_repository'),
Chris@17 82 $container->get('entity.memory_cache')
Chris@0 83 );
Chris@0 84 }
Chris@0 85
Chris@0 86 /**
Chris@0 87 * {@inheritdoc}
Chris@0 88 */
Chris@0 89 public function importDelete($name, Config $new_config, Config $old_config) {
Chris@0 90 // If the field storage has been deleted in the same import, the field will
Chris@0 91 // be deleted by then, and there is nothing left to do. Just return TRUE so
Chris@0 92 // that the file does not get written to active store.
Chris@0 93 if (!$old_config->get()) {
Chris@0 94 return TRUE;
Chris@0 95 }
Chris@0 96 return parent::importDelete($name, $new_config, $old_config);
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * {@inheritdoc}
Chris@0 101 */
Chris@0 102 public function loadByProperties(array $conditions = []) {
Chris@0 103 // Include deleted fields if specified in the $conditions parameters.
Chris@0 104 $include_deleted = isset($conditions['include_deleted']) ? $conditions['include_deleted'] : FALSE;
Chris@0 105 unset($conditions['include_deleted']);
Chris@0 106
Chris@0 107 $fields = [];
Chris@0 108
Chris@0 109 // Get fields stored in configuration. If we are explicitly looking for
Chris@0 110 // deleted fields only, this can be skipped, because they will be
Chris@14 111 // retrieved from the deleted fields repository below.
Chris@0 112 if (empty($conditions['deleted'])) {
Chris@0 113 if (isset($conditions['entity_type']) && isset($conditions['bundle']) && isset($conditions['field_name'])) {
Chris@0 114 // Optimize for the most frequent case where we do have a specific ID.
Chris@0 115 $id = $conditions['entity_type'] . '.' . $conditions['bundle'] . '.' . $conditions['field_name'];
Chris@0 116 $fields = $this->loadMultiple([$id]);
Chris@0 117 }
Chris@0 118 else {
Chris@0 119 // No specific ID, we need to examine all existing fields.
Chris@0 120 $fields = $this->loadMultiple();
Chris@0 121 }
Chris@0 122 }
Chris@0 123
Chris@14 124 // Merge deleted fields from the deleted fields repository if needed.
Chris@0 125 if ($include_deleted || !empty($conditions['deleted'])) {
Chris@14 126 $deleted_field_definitions = $this->deletedFieldsRepository->getFieldDefinitions();
Chris@14 127 foreach ($deleted_field_definitions as $id => $field_definition) {
Chris@14 128 if ($field_definition instanceof FieldConfigInterface) {
Chris@14 129 $fields[$id] = $field_definition;
Chris@0 130 }
Chris@0 131 }
Chris@0 132 }
Chris@0 133
Chris@0 134 // Collect matching fields.
Chris@0 135 $matching_fields = [];
Chris@0 136 foreach ($fields as $field) {
Chris@0 137 // Some conditions are checked against the field storage.
Chris@0 138 $field_storage = $field->getFieldStorageDefinition();
Chris@0 139
Chris@0 140 // Only keep the field if it matches all conditions.
Chris@0 141 foreach ($conditions as $key => $value) {
Chris@0 142 // Extract the actual value against which the condition is checked.
Chris@0 143 switch ($key) {
Chris@0 144 case 'field_name':
Chris@0 145 $checked_value = $field_storage->getName();
Chris@0 146 break;
Chris@0 147
Chris@0 148 case 'field_id':
Chris@0 149 case 'field_storage_uuid':
Chris@0 150 $checked_value = $field_storage->uuid();
Chris@0 151 break;
Chris@0 152
Chris@0 153 case 'uuid';
Chris@0 154 $checked_value = $field->uuid();
Chris@0 155 break;
Chris@0 156
Chris@0 157 case 'deleted';
Chris@0 158 $checked_value = $field->isDeleted();
Chris@0 159 break;
Chris@0 160
Chris@0 161 default:
Chris@0 162 $checked_value = $field->get($key);
Chris@0 163 break;
Chris@0 164 }
Chris@0 165
Chris@0 166 // Skip to the next field as soon as one condition does not match.
Chris@0 167 if ($checked_value != $value) {
Chris@0 168 continue 2;
Chris@0 169 }
Chris@0 170 }
Chris@0 171
Chris@0 172 // When returning deleted fields, key the results by UUID since they
Chris@0 173 // can include several fields with the same ID.
Chris@0 174 $key = $include_deleted ? $field->uuid() : $field->id();
Chris@0 175 $matching_fields[$key] = $field;
Chris@0 176 }
Chris@0 177
Chris@0 178 return $matching_fields;
Chris@0 179 }
Chris@0 180
Chris@0 181 }