You are here

public function EncryptedFieldStorageItem::encrypt in Field Encryption 3.0.x

Encrypts the field item.

File

src/Plugin/Field/FieldType/EncryptedFieldStorageItem.php, line 59

Class

EncryptedFieldStorageItem
Plugin implementation of the 'encrypted_field_storage' field type.

Namespace

Drupal\field_encrypt\Plugin\Field\FieldType

Code

public function encrypt() {

  // If the decrypted value is set encrypt it an overwrite.
  if ($this->decrypted_value !== NULL) {

    // Always use the encryption profile from configuration to encrypt the
    // field. This allows encryption keys to easily be changed.
    $encryption_profile = \Drupal::config('field_encrypt.settings')
      ->get('encryption_profile');
    $this->value = base64_encode(\Drupal::service('encryption')
      ->encrypt(serialize($this->decrypted_value), $this
      ->loadEncryptionProfile($encryption_profile)));
    if ($this
      ->get('encryption_profile')
      ->getValue() !== $encryption_profile) {
      $this->encryption_profile = $encryption_profile;
    }
  }
  else {
    $this->value = NULL;
  }
  $this->decrypted_value = NULL;
}