StateManager.php in Field Encryption 3.0.x
File
src/StateManager.php
View source
<?php
namespace Drupal\field_encrypt;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface;
use Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface;
use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\State\StateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
class StateManager {
protected $entityTypeManager;
protected $queueFactory;
protected $state;
protected $entitySchemaRepository;
protected $entityDefinitionUpdateManager;
protected $moduleHandler;
public function __construct(EntityTypeManagerInterface $entity_type_manager, QueueFactory $queue_factory, StateInterface $state, EntityLastInstalledSchemaRepositoryInterface $entity_schema_repository, EntityDefinitionUpdateManagerInterface $entity_definition_update_manager, ModuleHandlerInterface $module_handler) {
$this->entityTypeManager = $entity_type_manager;
$this->queueFactory = $queue_factory;
$this->state = $state;
$this->entitySchemaRepository = $entity_schema_repository;
$this->entityDefinitionUpdateManager = $entity_definition_update_manager;
$this->moduleHandler = $module_handler;
}
public function update() {
$old_entity_types = $this->state
->get('field_encrypt.entity_types', []);
$new_entity_types = $this
->getEntityTypes();
if ($old_entity_types === $new_entity_types) {
return;
}
foreach (array_diff($new_entity_types, $old_entity_types) as $type) {
$definition = static::getEncryptedFieldStorageDefinition();
$this->entityDefinitionUpdateManager
->installFieldStorageDefinition(ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME, $type, 'field_encrypt', $definition);
}
$this->state
->set('field_encrypt.entity_types', array_merge($old_entity_types, $new_entity_types));
$this->moduleHandler
->resetImplementations();
$this->entityTypeManager
->clearCachedDefinitions();
$this
->setEntityTypeCacheInformation($new_entity_types);
}
public function onFieldEncryptSettingsCacheChange() {
$this->entityTypeManager
->clearCachedDefinitions();
$this
->setEntityTypeCacheInformation($this->state
->get('field_encrypt.entity_types', []));
return $this;
}
protected function setEntityTypeCacheInformation(array $entity_type_ids) {
$entity_types = $this->entityTypeManager
->getDefinitions();
foreach ($entity_type_ids as $type) {
$last_installed_definition = $this->entitySchemaRepository
->getLastInstalledDefinition($type);
$last_installed_definition
->set('render_cache', $entity_types[$type]
->get('render_cache') ?? FALSE)
->set('persistent_cache', $entity_types[$type]
->get('persistent_cache') ?? FALSE);
$this->entitySchemaRepository
->setLastInstalledDefinition($last_installed_definition);
}
}
public function removeStorageFields() {
$queue = $this->queueFactory
->get('field_encrypt_update_entity_encryption');
if ($queue
->numberOfItems() > 0) {
return;
}
$old_entity_types = $this->state
->get('field_encrypt.entity_types', []);
$new_entity_types = $this
->getEntityTypes();
if ($old_entity_types === $new_entity_types) {
return;
}
$this->state
->set('field_encrypt.entity_types', $new_entity_types);
$this->moduleHandler
->resetImplementations();
foreach (array_diff($old_entity_types, $new_entity_types) as $type) {
$field = $this->entityDefinitionUpdateManager
->getFieldStorageDefinition(ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME, $type);
if ($field) {
$this->entityDefinitionUpdateManager
->uninstallFieldStorageDefinition($field);
}
}
}
public static function getEncryptedFieldStorageDefinition() {
return BaseFieldDefinition::create('encrypted_field_storage')
->setLabel(new TranslatableMarkup('Encrypted data'))
->setDescription(new TranslatableMarkup('Stores data from encrypted fields.'))
->setInternal(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE);
}
protected function getEntityTypes() {
$entity_types = [];
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type) {
if ($entity_type instanceof ContentEntityTypeInterface) {
$storage_class = $this->entityTypeManager
->createHandlerInstance($entity_type
->getStorageClass(), $entity_type);
if ($storage_class instanceof DynamicallyFieldableEntityStorageInterface) {
$entity_type_id = $entity_type
->id();
if ($this->entityTypeManager
->getStorage('field_encrypt_entity_type')
->load($entity_type_id)) {
$entity_types[$entity_type_id] = $entity_type_id;
continue;
}
$ids = $this->entityTypeManager
->getStorage('field_storage_config')
->getQuery()
->condition('id', $entity_type_id . '.', 'STARTS_WITH')
->execute();
$field_storages = $this->entityTypeManager
->getStorage('field_storage_config')
->loadMultiple($ids);
foreach ($field_storages as $storage) {
if ($storage
->getThirdPartySetting('field_encrypt', 'encrypt', FALSE) == TRUE) {
$entity_types[$entity_type_id] = $entity_type_id;
continue 2;
}
}
}
}
}
return $entity_types;
}
}