You are here

private function Anonymizer::anonymize in General Data Protection Regulation 7

Runs anonymize functionality against a field.

Parameters

array $field_info: The field to anonymise.

object|EntityInterface $entity: The parent entity.

Return value

array First element is success boolean, second element is the error message.

1 call to Anonymizer::anonymize()
Anonymizer::run in modules/gdpr_tasks/src/Anonymizer.php
Runs anonymization routines against a user.

File

modules/gdpr_tasks/src/Anonymizer.php, line 194

Class

Anonymizer
Anonymizes or removes field values for GDPR.

Code

private function anonymize(array $field_info, $entity) {
  $sanitizer_id = $this
    ->getSanitizerId($field_info, $entity);
  $field = $field_info['plugin']->property_name;
  if (!$sanitizer_id) {
    return array(
      FALSE,
      "Could not anonymize field {$field}. Please consider changing this field from 'anonymize' to 'remove', or register a custom sanitizer.",
      NULL,
    );
  }
  try {
    $plugin = gdpr_dump_get_sanitizer_plugins($sanitizer_id);
    $wrapper = entity_metadata_wrapper($field_info['entity_type'], $entity);
    $entity_property_info = $wrapper
      ->getPropertyInfo();
    if (function_exists($plugin['sanitize callback'])) {
      $type = isset($entity_property_info[$field]['type']) ? $entity_property_info[$field]['type'] : 'string';
      if ($type == 'text_formatted') {
        $wrapper->{$field} = [
          'value' => call_user_func($plugin['sanitize callback'], $field_info['value']),
          'safe_value' => '',
        ];
      }
      else {
        $wrapper->{$field} = call_user_func($plugin['sanitize callback'], $field_info['value']);
      }
      return array(
        TRUE,
        NULL,
        $sanitizer_id,
      );
    }
    else {
      throw new \Exception("No sanitizer found for field {$field}.");
    }
  } catch (\Exception $e) {
    return array(
      FALSE,
      $e
        ->getMessage(),
      NULL,
    );
  }
}