You are here

protected function FieldEncryptProcessEntities::processField in Field Encryption 8.2

Process a field.

Parameters

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

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

string $op: The operation to perform (encrypt / decrypt).

bool $update: Whether a batch re-encryption update is in progress.

array $original_encryption_settings: Original encryption settings - used when updating in batch.

2 calls to FieldEncryptProcessEntities::processField()
FieldEncryptProcessEntities::processEntity in src/FieldEncryptProcessEntities.php
Process an entity to either encrypt or decrypt its fields.
FieldEncryptProcessEntities::updateStoredField in src/FieldEncryptProcessEntities.php
Update the encryption settings on a stored field.

File

src/FieldEncryptProcessEntities.php, line 144

Class

FieldEncryptProcessEntities
Service class to process entities and fields for encryption.

Namespace

Drupal\field_encrypt

Code

protected function processField(ContentEntityInterface $entity, FieldItemListInterface $field, $op = 'encrypt', $update = FALSE, $original_encryption_settings = []) {

  // Check if field is properly set up and allows encryption.
  if (!$update && !$this
    ->checkField($field)) {
    return;
  }

  /* @var $definition \Drupal\Core\Field\BaseFieldDefinition */
  $definition = $field
    ->getFieldDefinition();

  /* @var $storage \Drupal\Core\Field\FieldConfigStorageBase */
  $storage = $definition
    ->get('fieldStorage');

  // If we are using the update flag, we always proceed.
  // The update flag is used when we are updating stored fields.
  if (!$update) {

    // Check if we are updating the field, in that case, skip it now (during
    // the initial entity load.
    if ($op == "decrypt" && $this->updatingStoredField === $definition
      ->get('field_name')) {
      return;
    }

    // Check if the field is encrypted.
    $encrypted = $storage
      ->getThirdPartySetting('field_encrypt', 'encrypt', FALSE);
    if (!$encrypted) {
      return;
    }
  }

  /* @var $field \Drupal\Core\Field\FieldItemList */
  $field_value = $field
    ->getValue();

  // Get encryption settings from storage, unless we are batch updating.
  if (isset($original_encryption_settings['encryption_profile'])) {
    $encryption_profile_id = $original_encryption_settings['encryption_profile'];
    $properties = $original_encryption_settings['properties'];
  }
  else {
    $encryption_profile_id = $storage
      ->getThirdPartySetting('field_encrypt', 'encryption_profile', []);
    $properties = $storage
      ->getThirdPartySetting('field_encrypt', 'properties', []);
  }
  $encryption_profile = $this->encryptionProfileManager
    ->getEncryptionProfile($encryption_profile_id);

  // Process the field with the given encryption provider.
  foreach ($field_value as $delta => &$value) {

    // Process each of the field properties that exist.
    foreach ($properties as $property_name) {
      if (isset($value[$property_name])) {
        $value[$property_name] = $this
          ->processValue($entity, $field, $delta, $property_name, $encryption_profile, $value[$property_name], $op);
      }
    }
  }

  // Set the new value.
  // We don't need to update the entity because setValue does that already.
  $field
    ->setValue($field_value);
}