annotate core/modules/field/src/FieldStorageConfigStorage.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@0 5 use Drupal\Component\Uuid\UuidInterface;
Chris@17 6 use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
Chris@0 7 use Drupal\Core\Config\Entity\ConfigEntityStorage;
Chris@0 8 use Drupal\Core\Entity\EntityInterface;
Chris@0 9 use Drupal\Core\Entity\EntityManagerInterface;
Chris@0 10 use Drupal\Core\Entity\EntityTypeInterface;
Chris@14 11 use Drupal\Core\Field\DeletedFieldsRepositoryInterface;
Chris@0 12 use Drupal\Core\Field\FieldTypePluginManagerInterface;
Chris@0 13 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 14 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 15 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 16 use Drupal\Core\Extension\ModuleHandlerInterface;
Chris@0 17
Chris@0 18 /**
Chris@14 19 * Storage handler for "field storage" configuration entities.
Chris@0 20 */
Chris@0 21 class FieldStorageConfigStorage extends ConfigEntityStorage {
Chris@0 22
Chris@0 23 /**
Chris@0 24 * The module handler.
Chris@0 25 *
Chris@0 26 * @var \Drupal\Core\Extension\ModuleHandlerInterface
Chris@0 27 */
Chris@0 28 protected $moduleHandler;
Chris@0 29
Chris@0 30 /**
Chris@0 31 * The entity manager.
Chris@0 32 *
Chris@0 33 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 34 */
Chris@0 35 protected $entityManager;
Chris@0 36
Chris@0 37 /**
Chris@0 38 * The field type plugin manager.
Chris@0 39 *
Chris@0 40 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
Chris@0 41 */
Chris@0 42 protected $fieldTypeManager;
Chris@0 43
Chris@0 44 /**
Chris@14 45 * The deleted fields repository.
Chris@14 46 *
Chris@14 47 * @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface
Chris@14 48 */
Chris@14 49 protected $deletedFieldsRepository;
Chris@14 50
Chris@14 51 /**
Chris@0 52 * Constructs a FieldStorageConfigStorage object.
Chris@0 53 *
Chris@0 54 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 55 * The entity type definition.
Chris@0 56 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 57 * The config factory service.
Chris@0 58 * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
Chris@0 59 * The UUID service.
Chris@0 60 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@0 61 * The language manager.
Chris@0 62 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 63 * The entity manager.
Chris@0 64 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
Chris@0 65 * The module handler.
Chris@14 66 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
Chris@0 67 * The field type plugin manager.
Chris@14 68 * @param \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository
Chris@14 69 * The deleted fields repository.
Chris@17 70 * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache
Chris@17 71 * The memory cache.
Chris@0 72 */
Chris@17 73 public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager, DeletedFieldsRepositoryInterface $deleted_fields_repository, MemoryCacheInterface $memory_cache) {
Chris@17 74 parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager, $memory_cache);
Chris@0 75 $this->entityManager = $entity_manager;
Chris@0 76 $this->moduleHandler = $module_handler;
Chris@0 77 $this->fieldTypeManager = $field_type_manager;
Chris@14 78 $this->deletedFieldsRepository = $deleted_fields_repository;
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * {@inheritdoc}
Chris@0 83 */
Chris@0 84 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
Chris@0 85 return new static(
Chris@0 86 $entity_type,
Chris@0 87 $container->get('config.factory'),
Chris@0 88 $container->get('uuid'),
Chris@0 89 $container->get('language_manager'),
Chris@0 90 $container->get('entity.manager'),
Chris@0 91 $container->get('module_handler'),
Chris@14 92 $container->get('plugin.manager.field.field_type'),
Chris@17 93 $container->get('entity_field.deleted_fields_repository'),
Chris@17 94 $container->get('entity.memory_cache')
Chris@0 95 );
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * {@inheritdoc}
Chris@0 100 */
Chris@0 101 public function loadByProperties(array $conditions = []) {
Chris@0 102 // Include deleted fields if specified in the $conditions parameters.
Chris@0 103 $include_deleted = isset($conditions['include_deleted']) ? $conditions['include_deleted'] : FALSE;
Chris@0 104 unset($conditions['include_deleted']);
Chris@0 105
Chris@0 106 /** @var \Drupal\field\FieldStorageConfigInterface[] $storages */
Chris@0 107 $storages = [];
Chris@0 108
Chris@0 109 // Get field storages living in configuration. If we are explicitly looking
Chris@0 110 // for deleted storages 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['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['field_name'];
Chris@0 116 $storages = $this->loadMultiple([$id]);
Chris@0 117 }
Chris@0 118 else {
Chris@0 119 // No specific ID, we need to examine all existing storages.
Chris@0 120 $storages = $this->loadMultiple();
Chris@0 121 }
Chris@0 122 }
Chris@0 123
Chris@14 124 // Merge deleted field storage definitions from the deleted fields
Chris@14 125 // repository if needed.
Chris@0 126 if ($include_deleted || !empty($conditions['deleted'])) {
Chris@14 127 $deleted_storage_definitions = $this->deletedFieldsRepository->getFieldStorageDefinitions();
Chris@14 128 foreach ($deleted_storage_definitions as $id => $field_storage_definition) {
Chris@14 129 if ($field_storage_definition instanceof FieldStorageConfigInterface) {
Chris@14 130 $storages[$id] = $field_storage_definition;
Chris@14 131 }
Chris@0 132 }
Chris@0 133 }
Chris@0 134
Chris@0 135 // Collect matching fields.
Chris@0 136 $matches = [];
Chris@0 137 foreach ($storages as $field) {
Chris@0 138 foreach ($conditions as $key => $value) {
Chris@0 139 // Extract the actual value against which the condition is checked.
Chris@0 140 $checked_value = $field->get($key);
Chris@0 141 // Skip to the next field as soon as one condition does not match.
Chris@0 142 if ($checked_value != $value) {
Chris@0 143 continue 2;
Chris@0 144 }
Chris@0 145 }
Chris@0 146
Chris@0 147 // When returning deleted fields, key the results by UUID since they can
Chris@0 148 // include several fields with the same ID.
Chris@0 149 $key = $include_deleted ? $field->uuid() : $field->id();
Chris@0 150 $matches[$key] = $field;
Chris@0 151 }
Chris@0 152
Chris@0 153 return $matches;
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * {@inheritdoc}
Chris@0 158 */
Chris@0 159 protected function mapFromStorageRecords(array $records) {
Chris@0 160 foreach ($records as $id => &$record) {
Chris@0 161 $class = $this->fieldTypeManager->getPluginClass($record['type']);
Chris@0 162 if (empty($class)) {
Chris@0 163 $config_id = $this->getPrefix() . $id;
Chris@0 164 throw new \RuntimeException("Unable to determine class for field type '{$record['type']}' found in the '$config_id' configuration");
Chris@0 165 }
Chris@0 166 $record['settings'] = $class::storageSettingsFromConfigData($record['settings']);
Chris@0 167 }
Chris@0 168 return parent::mapFromStorageRecords($records);
Chris@0 169 }
Chris@0 170
Chris@0 171 /**
Chris@0 172 * {@inheritdoc}
Chris@0 173 */
Chris@0 174 protected function mapToStorageRecord(EntityInterface $entity) {
Chris@0 175 $record = parent::mapToStorageRecord($entity);
Chris@0 176 $class = $this->fieldTypeManager->getPluginClass($record['type']);
Chris@0 177 $record['settings'] = $class::storageSettingsToConfigData($record['settings']);
Chris@0 178 return $record;
Chris@0 179 }
Chris@0 180
Chris@0 181 }