You are here

protected function ProcessEntities::encryptFieldValue in Field Encryption 3.0.x

Moves the unencrypted value to the encrypted field storage.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity to process.

\Drupal\Core\Field\FieldItemListInterface $field: The field to process.

int $delta: The field delta.

string $property_name: The name of the property.

mixed $value: The value to decrypt.

Return value

mixed The encrypted field database value.

1 call to ProcessEntities::encryptFieldValue()
ProcessEntities::encryptField in src/ProcessEntities.php
Encrypts a field.

File

src/ProcessEntities.php, line 242

Class

ProcessEntities
Service class to process entities and fields for encryption.

Namespace

Drupal\field_encrypt

Code

protected function encryptFieldValue(ContentEntityInterface $entity, FieldItemListInterface $field, int $delta, string $property_name, $value = '') {

  // Do not modify empty strings.
  if ($value === '') {
    return '';
  }
  $storage = $entity
    ->get(static::ENCRYPTED_FIELD_STORAGE_NAME)->decrypted_value ?? [];

  // Return value to store for unencrypted property.
  // We can't set this to NULL, because then the field values are not
  // saved, so we can't replace them with their unencrypted value on load.
  $placeholder_value = $this
    ->getUnencryptedPlaceholderValue($entity, $field, $property_name);
  if ($placeholder_value !== $value) {
    $storage[$field
      ->getName()][$delta][$property_name] = $value;
    $entity
      ->get(static::ENCRYPTED_FIELD_STORAGE_NAME)->decrypted_value = $storage;
    return $placeholder_value;
  }

  // If not allowed, but we still have an encrypted value remove it.
  if (isset($storage[$field
    ->getName()][$delta][$property_name])) {
    unset($storage[$field
      ->getName()][$delta][$property_name]);
    $entity
      ->get(static::ENCRYPTED_FIELD_STORAGE_NAME)->decrypted_value = $storage;
  }
  return $value;
}