RightToAccessEntityTraversal.php in General Data Protection Regulation 3.0.x
File
modules/gdpr_tasks/src/Traversal/RightToAccessEntityTraversal.php
View source
<?php
namespace Drupal\gdpr_tasks\Traversal;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\gdpr_fields\Entity\GdprField;
use Drupal\gdpr_fields\Entity\GdprFieldConfigEntity;
use Drupal\gdpr_fields\EntityTraversal;
use function implode;
use function in_array;
use function pathinfo;
class RightToAccessEntityTraversal extends EntityTraversal {
private $assets = [];
protected function processEntity(FieldableEntityInterface $entity, GdprFieldConfigEntity $config, $row_id, GdprField $parent_config = NULL) {
$entityType = $entity
->getEntityTypeId();
$fields = $this->entityFieldManager
->getFieldDefinitions($entityType, $entity
->bundle());
$fieldConfigs = $config
->getFieldsForBundle($entity
->bundle());
foreach ($fields as $fieldId => $field) {
$fieldConfig = isset($fieldConfigs[$fieldId]) ? $fieldConfigs[$fieldId] : NULL;
if ($fieldConfig === NULL || !$fieldConfig->enabled || !in_array($fieldConfig->rta, [
'inc',
'maybe',
])) {
continue;
}
$pluginName = "{$entityType}|{$entity->bundle()}|{$fieldId}";
$filename = 'main';
if ($parent_config) {
$filename = !empty($parent_config->sarsFilename) ? $parent_config->sarsFilename : $filename;
}
$fieldValue = $this
->getFieldValue($entity, $field, $fieldId);
$data = [
'plugin_name' => $pluginName,
'entity_type' => $entityType,
'entity_id' => $entity
->id(),
'file' => $filename,
'row_id' => $row_id,
'label' => $field
->getLabel(),
'value' => $fieldValue,
'notes' => $fieldConfig->notes,
'rta' => $fieldConfig->rta,
];
$this->results["{$pluginName}|{$entity->id()}"] = $data;
}
}
protected function getFieldValue(FieldableEntityInterface $entity, FieldDefinitionInterface $field, $fieldId) {
$labels = [];
if ($entity->{$fieldId} instanceof EntityReferenceFieldItemList) {
if ($field
->getSetting('target_type') === 'file') {
foreach ($entity->{$fieldId}
->referencedEntities() as $file) {
$this->assets[] = [
'target_id' => $file
->id(),
'display' => 1,
];
$labels[] = "assets/{$file->id()}." . pathinfo($file
->getFileUri(), PATHINFO_EXTENSION);
}
}
else {
foreach ($entity->{$fieldId}
->referencedEntities() as $referenced_entity) {
if ($referenced_entity
->label()) {
$labels[] = "{$referenced_entity->label()} [{$referenced_entity->id()}]";
}
else {
$labels[] = $referenced_entity
->id();
}
}
}
}
else {
$labels[] = $entity
->get($fieldId)
->getString();
}
return implode(', ', $labels);
}
}