You are here

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

Encrypts a field.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity being encrypted.

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

1 call to ProcessEntities::encryptField()
ProcessEntities::encryptEntity in src/ProcessEntities.php
Encrypts an entity's encrypted fields.

File

src/ProcessEntities.php, line 135

Class

ProcessEntities
Service class to process entities and fields for encryption.

Namespace

Drupal\field_encrypt

Code

protected function encryptField(ContentEntityInterface $entity, FieldItemListInterface $field) {
  $definition = $field
    ->getFieldDefinition();
  $storage = $definition
    ->getFieldStorageDefinition();
  $field_value = $field
    ->getValue();

  // Get encryption settings from storage.
  if ($storage
    ->isBaseField()) {
    $properties = $storage
      ->getSetting('field_encrypt.properties') ?? [];
  }
  else {

    /** @var \Drupal\field\FieldStorageConfigInterface $storage */
    $properties = $storage
      ->getThirdPartySetting('field_encrypt', 'properties', []);
  }

  // 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
          ->encryptFieldValue($entity, $field, $delta, $property_name, $value[$property_name]);
      }
    }
  }

  // Set the new value. Calling setValue() updates the entity too.
  $field
    ->setValue($field_value);
}