You are here

public function ConfigSubscriber::onConfigSave in Field Encryption 8.2

Same name and namespace in other branches
  1. 3.0.x src/EventSubscriber/ConfigSubscriber.php \Drupal\field_encrypt\EventSubscriber\ConfigSubscriber::onConfigSave()

React on the configuration save event.

@todo why is this not just using hook_field_storage_update?

Parameters

ConfigCrudEvent $event: The configuration event.

File

src/EventSubscriber/ConfigSubscriber.php, line 115

Class

ConfigSubscriber
Updates existing data when field encryption settings are updated.

Namespace

Drupal\field_encrypt\EventSubscriber

Code

public function onConfigSave(ConfigCrudEvent $event) {
  $config = $event
    ->getConfig();
  if (substr($config
    ->getName(), 0, 14) == 'field.storage.') {

    // Get the original field_encrypt configuration.
    $original_config = $config
      ->getOriginal('third_party_settings.field_encrypt');

    // Update the uncacheable entity types list.
    $this
      ->setUncacheableEntityTypes();

    // Update existing entities, if data encryption settings changed.
    if ($this
      ->encryptionConfigChanged($config)) {

      // Get the entity type and field from the changed config key.
      $storage_name = substr($config
        ->getName(), 14);
      list($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)
            ->hasKey('revision')) {
            $query
              ->allRevisions();
          }
          $entity_ids = $query
            ->execute();
          if (!empty($entity_ids)) {

            // Call the Queue API and add items for processing.

            /** @var \Drupal\Core\Queue\QueueInterface $queue */
            $queue = $this->queueFactory
              ->get('cron_encrypted_field_update');
            foreach (array_keys($entity_ids) as $entity_id) {
              $data = [
                "entity_id" => $entity_id,
                "field_name" => $field_name,
                "entity_type" => $entity_type,
                "original_config" => $original_config,
              ];
              $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.', array(
            ':url' => Url::fromRoute('field_encrypt.field_update')
              ->toString(),
          )));
        }
      }
    }
  }
}