You are here

protected function RightToBeForgottenEntityTraversal::processEntity in General Data Protection Regulation 8

Same name and namespace in other branches
  1. 8.2 modules/gdpr_tasks/src/Traversal/RightToBeForgottenEntityTraversal.php \Drupal\gdpr_tasks\Traversal\RightToBeForgottenEntityTraversal::processEntity()
  2. 3.0.x modules/gdpr_tasks/src/Traversal/RightToBeForgottenEntityTraversal.php \Drupal\gdpr_tasks\Traversal\RightToBeForgottenEntityTraversal::processEntity()

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Core\Entity\EntityStorageException

Overrides EntityTraversal::processEntity

File

modules/gdpr_tasks/src/Traversal/RightToBeForgottenEntityTraversal.php, line 89

Class

RightToBeForgottenEntityTraversal
Entity traversal used for Right to be Forgotten requests.

Namespace

Drupal\gdpr_tasks\Traversal

Code

protected function processEntity(FieldableEntityInterface $entity, GdprFieldConfigEntity $config, $row_id, GdprField $parent_config = NULL) {
  $entity_success = TRUE;
  $entity_type = $entity
    ->getEntityTypeId();
  $fields = $this->entityFieldManager
    ->getFieldDefinitions($entity_type, $entity
    ->bundle());
  $field_configs = $config
    ->getFieldsForBundle($entity
    ->bundle());

  // Re-load a fresh copy of the entity from storage so we don't
  // end up modifying any other references to the entity in memory.

  /* @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
  $entity = $this->entityTypeManager
    ->getStorage($entity_type)
    ->loadUnchanged($entity
    ->id());
  foreach ($fields as $field_id => $field_definition) {
    $field_config = isset($field_configs[$field_id]) ? $field_configs[$field_id] : NULL;

    // If the field is not configured, not enabled,
    // or not enabled for RTF, then skip it.
    if ($field_config === NULL || !$field_config->enabled || !in_array($field_config->rtf, [
      'anonymize',
      'remove',
      'maybe',
    ])) {
      continue;
    }
    $mode = $field_config->rtf;
    $field = $entity
      ->get($field_id);
    $success = TRUE;
    $msg = NULL;
    $anonymizer = '';
    if ($mode == 'anonymize') {
      list($success, $msg, $anonymizer) = $this
        ->anonymize($field, $field_definition, $field_config);
    }
    elseif ($mode == 'remove') {
      list($success, $msg, $should_delete) = $this
        ->remove($field, $field_config, $entity);
    }
    if ($success === TRUE) {
      $this->results['log'][] = [
        'entity_id' => $entity
          ->id(),
        'entity_type' => $entity_type . '.' . $entity
          ->bundle(),
        'field_name' => $field
          ->getName(),
        'action' => $mode,
        'anonymizer' => $anonymizer,
      ];
    }
    else {

      // Could not anonymize/remove field. Record to errors list.
      // Prevent entity from being saved.
      $entity_success = FALSE;
      $this->results['errors'][] = $msg;
    }
  }
  if ($entity_success) {
    if (isset($should_delete) && $should_delete) {
      $this->results['to_delete'][] = $entity;
    }
    else {
      $this->results['successes'][] = $entity;
    }
  }
  else {
    $this->results['failures'][] = $entity;
  }
}