FieldStorageConfigStorage.php in Drupal 8
File
core/modules/field/src/FieldStorageConfigStorage.php
View source
<?php
namespace Drupal\field;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\DeletedFieldsRepositoryInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
class FieldStorageConfigStorage extends ConfigEntityStorage {
use DeprecatedServicePropertyTrait;
protected $deprecatedProperties = [
'entityManager' => 'entity.manager',
];
protected $moduleHandler;
protected $entityTypeManager;
protected $fieldTypeManager;
protected $deletedFieldsRepository;
public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager, DeletedFieldsRepositoryInterface $deleted_fields_repository, MemoryCacheInterface $memory_cache) {
parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager, $memory_cache);
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->fieldTypeManager = $field_type_manager;
$this->deletedFieldsRepository = $deleted_fields_repository;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('config.factory'), $container
->get('uuid'), $container
->get('language_manager'), $container
->get('entity_type.manager'), $container
->get('module_handler'), $container
->get('plugin.manager.field.field_type'), $container
->get('entity_field.deleted_fields_repository'), $container
->get('entity.memory_cache'));
}
public function loadByProperties(array $conditions = []) {
$include_deleted = isset($conditions['include_deleted']) ? $conditions['include_deleted'] : FALSE;
unset($conditions['include_deleted']);
$storages = [];
if (empty($conditions['deleted'])) {
if (isset($conditions['entity_type']) && isset($conditions['field_name'])) {
$id = $conditions['entity_type'] . $conditions['field_name'];
$storages = $this
->loadMultiple([
$id,
]);
}
else {
$storages = $this
->loadMultiple();
}
}
if ($include_deleted || !empty($conditions['deleted'])) {
$deleted_storage_definitions = $this->deletedFieldsRepository
->getFieldStorageDefinitions();
foreach ($deleted_storage_definitions as $id => $field_storage_definition) {
if ($field_storage_definition instanceof FieldStorageConfigInterface) {
$storages[$id] = $field_storage_definition;
}
}
}
$matches = [];
foreach ($storages as $field) {
foreach ($conditions as $key => $value) {
$checked_value = $field
->get($key);
if ($checked_value != $value) {
continue 2;
}
}
$key = $include_deleted ? $field
->uuid() : $field
->id();
$matches[$key] = $field;
}
return $matches;
}
protected function mapFromStorageRecords(array $records) {
foreach ($records as $id => &$record) {
try {
$class = $this->fieldTypeManager
->getPluginClass($record['type']);
} catch (PluginNotFoundException $e) {
$config_id = $this
->getPrefix() . $id;
throw new PluginNotFoundException($record['type'], "Unable to determine class for field type '{$record['type']}' found in the '{$config_id}' configuration", $e
->getCode(), $e);
}
$record['settings'] = $class::storageSettingsFromConfigData($record['settings']);
}
return parent::mapFromStorageRecords($records);
}
protected function mapToStorageRecord(EntityInterface $entity) {
$record = parent::mapToStorageRecord($entity);
$class = $this->fieldTypeManager
->getPluginClass($record['type']);
$record['settings'] = $class::storageSettingsToConfigData($record['settings']);
return $record;
}
}