GDPRCollector.php in General Data Protection Regulation 3.0.x
File
modules/gdpr_fields/src/GDPRCollector.php
View source
<?php
namespace Drupal\gdpr_fields;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Link;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\gdpr_fields\Entity\GdprFieldConfigEntity;
use function in_array;
use function stripos;
class GDPRCollector {
protected $entityTypeManager;
private $entityFieldManager;
private $bundleInfo;
public function __construct(EntityTypeManager $entityTypeManager, EntityFieldManagerInterface $entityFieldManager, EntityTypeBundleInfoInterface $bundleInfo) {
$this->entityTypeManager = $entityTypeManager;
$this->entityFieldManager = $entityFieldManager;
$this->bundleInfo = $bundleInfo;
}
public function listFields(EntityTypeInterface $entityType, $bundleId, array $filters) {
$bundleType = $entityType
->getBundleEntityType();
$gdprSettings = GdprFieldConfigEntity::load($entityType
->id());
if ($entityType
->id() == 'commerce_order_item') {
return [];
}
$fieldDefinitions = $this->entityFieldManager
->getFieldDefinitions($entityType
->id(), $bundleId);
$fields = [];
if ($filters['empty'] && $gdprSettings === NULL) {
return $fields;
}
$hasAtLeastOneConfiguredField = FALSE;
foreach ($fieldDefinitions as $fieldId => $fieldDefinition) {
$key = "{$entityType->id()}.{$bundleId}.{$fieldId}";
$route_name = 'gdpr_fields.edit_field';
$route_params = [
'entity_type' => $entityType
->id(),
'bundle_name' => $bundleId,
'field_name' => $fieldId,
];
if (isset($bundleType)) {
$route_params[$bundleType] = $bundleId;
}
$rta = '0';
$rtf = '0';
$label = $fieldDefinition
->getLabel();
if ($filters['search'] && (!stripos($label, $filters['search']) || !stripos($fieldDefinition
->getName(), $filters['search']))) {
continue;
}
$is_id = $entityType
->getKey('id') === $fieldId;
$fields[$key] = [
'title' => $label,
'type' => $is_id ? 'primary_key' : $fieldDefinition
->getType(),
'rta' => 'Not Configured',
'rtf' => 'Not Configured',
'notes' => '',
'edit' => '',
'is_id' => $is_id,
];
if ($entityType
->get('field_ui_base_route')) {
$fields[$key]['edit'] = Link::createFromRoute('edit', $route_name, $route_params);
}
if ($gdprSettings !== NULL) {
$field_settings = $gdprSettings
->getField($bundleId, $fieldId);
if ($field_settings->enabled) {
$hasAtLeastOneConfiguredField = TRUE;
$rta = $field_settings->rta;
$rtf = $field_settings->rtf;
$fields[$key]['rta'] = $field_settings
->rtaDescription();
$fields[$key]['rtf'] = $field_settings
->rtfDescription();
$fields[$key]['notes'] = $field_settings->notes;
}
}
if (!empty($filters['rtf']) && !in_array($rtf, $filters['rtf'], FALSE)) {
unset($fields[$key]);
}
if (!empty($filters['rta']) && !in_array($rta, $filters['rta'], FALSE)) {
unset($fields[$key]);
}
}
if ($filters['empty'] && !$hasAtLeastOneConfiguredField) {
return [];
}
return $fields;
}
public function getBundles($entityType_id) {
$all_bundles = $this->bundleInfo
->getAllBundleInfo();
$bundles = isset($all_bundles[$entityType_id]) ? $all_bundles[$entityType_id] : [
$entityType_id => [],
];
return $bundles;
}
}
Classes
Name |
Description |
GDPRCollector |
Defines a helper class for stuff related to views data. |