You are here

protected function RightToAccessEntityTraversal::getFieldValue 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::getFieldValue()
  2. 3.0.x modules/gdpr_tasks/src/Traversal/RightToAccessEntityTraversal.php \Drupal\gdpr_tasks\Traversal\RightToAccessEntityTraversal::getFieldValue()

Gets the field value, taking into account file references.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The current entity being processed.

\Drupal\Core\Field\FieldDefinitionInterface $field: Field definition.

string $field_id: Field ID.

Return value

string Field value

1 call to RightToAccessEntityTraversal::getFieldValue()
RightToAccessEntityTraversal::processEntity in modules/gdpr_tasks/src/Traversal/RightToAccessEntityTraversal.php
Handles the entity.

File

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

Class

RightToAccessEntityTraversal
Entity traversal for performing Right to Access requests.

Namespace

Drupal\gdpr_tasks\Traversal

Code

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;
}