You are here

public function FieldEncryptProcessEntities::updateStoredField in Field Encryption 8.2

Update the encryption settings on a stored field.

Parameters

string $field_name: The field name to update.

string $field_entity_type: The entity type to update.

array $original_encryption_settings: Array with original encryption settings to decrypt current values.

int $entity_id: The entity (revision) ID to update.

Overrides FieldEncryptProcessEntitiesInterface::updateStoredField

File

src/FieldEncryptProcessEntities.php, line 375

Class

FieldEncryptProcessEntities
Service class to process entities and fields for encryption.

Namespace

Drupal\field_encrypt

Code

public function updateStoredField($field_name, $field_entity_type, $original_encryption_settings, $entity_id) {

  // Before we load entities, we have to disable the encryption setting.
  // Otherwise, the act of loading the entity triggers an improper decryption
  // which messes up the batch encryption.
  $this->updatingStoredField = $field_name;
  $entity_storage = $this->entityManager
    ->getStorage($field_entity_type);

  // Check if entity allows revisions.
  if ($this->entityManager
    ->getDefinition($field_entity_type)
    ->hasKey('revision')) {
    $entity = $entity_storage
      ->loadRevision($entity_id);
  }
  else {
    $entity = $entity_storage
      ->load($entity_id);
  }

  // Process all language variants of the entity.
  $languages = $entity
    ->getTranslationLanguages();
  foreach ($languages as $language) {
    $entity = $entity
      ->getTranslation($language
      ->getId());
    $field = $entity
      ->get($field_name);

    // Decrypt with original settings, if available.
    if (!empty($original_encryption_settings)) {
      $this
        ->processField($entity, $field, 'decrypt', TRUE, $original_encryption_settings);
    }
  }
  $entity
    ->save();

  // Deactivate encryption if field is no longer encrypted.
  if (!$this
    ->checkField($field)) {
    $this->encryptedFieldValueManager
      ->deleteEntityEncryptedFieldValuesForField($entity, $field_name);
  }
}