protected function ConfigSubscriber::onFieldStorageChange in Field Encryption 3.0.x
Reacts to changes in field.storage.*.
Parameters
\Drupal\Core\Config\Config $config: The field storage config.
1 call to ConfigSubscriber::onFieldStorageChange()
- ConfigSubscriber::onConfigSave in src/
EventSubscriber/ ConfigSubscriber.php - React on the configuration save event.
File
- src/
EventSubscriber/ ConfigSubscriber.php, line 111
Class
- ConfigSubscriber
- Updates existing data when field encryption settings are updated.
Namespace
Drupal\field_encrypt\EventSubscriberCode
protected function onFieldStorageChange(Config $config) {
// Get the original field_encrypt configuration.
$original_config = $config
->getOriginal('third_party_settings.field_encrypt');
if ($config
->get('third_party_settings.field_encrypt') === $original_config) {
return;
}
// Update existing entities, if data encryption settings changed.
// Get the entity type and field from the changed config key.
$storage_name = substr($config
->getName(), 14);
[
$entity_type,
$field_name,
] = explode('.', $storage_name, 2);
// Load the FieldStorageConfig entity that was updated.
$field_storage_config = FieldStorageConfig::loadByName($entity_type, $field_name);
if ($field_storage_config) {
if ($field_storage_config
->hasData()) {
// Get entities that need updating, because they contain the field
// that has its field encryption settings updated.
$query = $this->entityTypeManager
->getStorage($entity_type)
->getQuery();
// Check if the field is present.
$query
->exists($field_name);
// Make sure to get all revisions for revisionable entities.
if ($this->entityTypeManager
->getDefinition($entity_type)
->isRevisionable()) {
$query
->allRevisions();
}
$entity_ids = $query
->execute();
if (!empty($entity_ids)) {
// Call the Queue API and add items for processing.
$queue = $this->queueFactory
->get('field_encrypt_update_entity_encryption');
$data = [
'entity_type' => $entity_type,
];
foreach (array_keys($entity_ids) as $entity_id) {
$data['entity_id'] = $entity_id;
$queue
->createItem($data);
}
}
$this->messenger
->addMessage($this
->t('Updates to entities with existing data for this field have been queued to be processed. You should immediately <a href=":url">run this process manually</a>. Alternatively, the updates will be performed automatically by cron.', [
':url' => Url::fromRoute('field_encrypt.process_queue')
->toString(),
]));
}
}
// Update the field_encrypt module's state.
$this->stateManager
->update();
}