You are here

class RightToAccessEntityTraversal in General Data Protection Regulation 8

Same name and namespace in other branches
  1. 8.2 modules/gdpr_tasks/src/Traversal/RightToAccessEntityTraversal.php \Drupal\gdpr_tasks\Traversal\RightToAccessEntityTraversal
  2. 3.0.x modules/gdpr_tasks/src/Traversal/RightToAccessEntityTraversal.php \Drupal\gdpr_tasks\Traversal\RightToAccessEntityTraversal

Entity traversal for performing Right to Access requests.

@package Drupal\gdpr_tasks

Hierarchy

Expanded class hierarchy of RightToAccessEntityTraversal

1 string reference to 'RightToAccessEntityTraversal'
gdpr_tasks.services.yml in modules/gdpr_tasks/gdpr_tasks.services.yml
modules/gdpr_tasks/gdpr_tasks.services.yml

File

modules/gdpr_tasks/src/Traversal/RightToAccessEntityTraversal.php, line 17

Namespace

Drupal\gdpr_tasks\Traversal
View source
class RightToAccessEntityTraversal extends EntityTraversal {
  private $assets = [];

  /**
   * {@inheritdoc}
   */
  protected function processEntity(FieldableEntityInterface $entity, GdprFieldConfigEntity $config, $row_id, GdprField $parent_config = NULL) {
    $entity_type = $entity
      ->getEntityTypeId();
    $fields = $this->entityFieldManager
      ->getFieldDefinitions($entity_type, $entity
      ->bundle());
    $field_configs = $config
      ->getFieldsForBundle($entity
      ->bundle());
    foreach ($fields as $field_id => $field) {
      $field_config = isset($field_configs[$field_id]) ? $field_configs[$field_id] : NULL;

      // If the field is not configured, not enabled,
      // or not enabled for RTA, then skip it.
      if ($field_config === NULL || !$field_config->enabled || !in_array($field_config->rta, [
        'inc',
        'maybe',
      ])) {
        continue;
      }
      $plugin_name = "{$entity_type}|{$entity->bundle()}|{$field_id}";
      $filename = 'main';
      if ($parent_config) {
        $filename = !empty($parent_config->sarsFilename) ? $parent_config->sarsFilename : $filename;
      }
      $field_value = $this
        ->getFieldValue($entity, $field, $field_id);
      $data = [
        'plugin_name' => $plugin_name,
        'entity_type' => $entity_type,
        'entity_id' => $entity
          ->id(),
        'file' => $filename,
        'row_id' => $row_id,
        'label' => $field
          ->getLabel(),
        'value' => $field_value,
        'notes' => $field_config->notes,
        'rta' => $field_config->rta,
      ];
      $this->results["{$plugin_name}|{$entity->id()}"] = $data;
    }
  }

  /**
   * Gets the field value, taking into account file references.
   *
   * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
   *   The current entity being processed.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field
   *   Field definition.
   * @param string $field_id
   *   Field ID.
   *
   * @return string
   *   Field value
   */
  protected function getFieldValue(FieldableEntityInterface $entity, FieldDefinitionInterface $field, $field_id) {

    // Special handling for file references.
    // For files, we want to add to the assets collection.
    $labels = [];
    if ($entity->{$field_id} instanceof EntityReferenceFieldItemList && $field
      ->getSetting('target_type') == 'file') {

      /* @var \Drupal\file\Entity\File $file */
      foreach ($entity->{$field_id}
        ->referencedEntities() as $file) {
        $this->assets[] = [
          'target_id' => $file
            ->id(),
          'display' => 1,
        ];
        $labels[] = "assets/{$file->id()}." . pathinfo($file
          ->getFileUri(), PATHINFO_EXTENSION);
      }
    }
    elseif ($entity->{$field_id} instanceof EntityReferenceFieldItemList) {

      /* @var \Drupal\Core\Entity\EntityInterface $referenced_entity */
      foreach ($entity->{$field_id}
        ->referencedEntities() as $referenced_entity) {
        if ($referenced_entity
          ->label()) {
          $labels[] = "{$referenced_entity->label()} [{$referenced_entity->id()}]";
        }
        else {
          $labels[] = $referenced_entity
            ->id();
        }
      }
    }
    else {
      $labels[] = $entity
        ->get($field_id)
        ->getString();
    }
    $field_value = implode(', ', $labels);
    return $field_value;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityTraversal::$baseEntity protected property The starting entity for the traversal.
EntityTraversal::$configStorage protected property Entity storage for GDPR config entities.
EntityTraversal::$entities protected property The processed entities.
EntityTraversal::$entityFieldManager protected property Entity field manager.
EntityTraversal::$entityTypeManager protected property Entity type manager.
EntityTraversal::$results protected property The results of the traversal.
EntityTraversal::$reverseRelationshipFields private property Reverse relationship information.
EntityTraversal::$success protected property Whether or not the traversal has happened successfully.
EntityTraversal::create public static function Creates an instance of the traversal for this specific entity. Overrides EntityTraversalContainerInjectionInterface::create 1
EntityTraversal::doTraversalRecursive protected function Traverses the entity relationship tree.
EntityTraversal::getAllReverseRelationships protected function Gets all reverse relationships configured in the system.
EntityTraversal::getBundleLabel protected function Gets the entity bundle label. Useful for display traversal.
EntityTraversal::getEntities public function Get the calculated actual calling points.
EntityTraversal::getResults public function Get the traversal results. Overrides EntityTraversalInterface::getResults
EntityTraversal::traverse public function Traverses the entity relationship tree if not done before. Overrides EntityTraversalInterface::traverse
EntityTraversal::traverseEntity protected function Traverses the entity relationship tree. 1
EntityTraversal::__construct public function EntityTraversal constructor. 1
RightToAccessEntityTraversal::$assets private property
RightToAccessEntityTraversal::getFieldValue protected function Gets the field value, taking into account file references.
RightToAccessEntityTraversal::processEntity protected function Handles the entity. Overrides EntityTraversal::processEntity