You are here

public function GDPRController::fieldsList in General Data Protection Regulation 8.2

Same name and namespace in other branches
  1. 8 modules/gdpr_fields/src/Controller/GDPRController.php \Drupal\gdpr_fields\Controller\GDPRController::fieldsList()
  2. 3.0.x modules/gdpr_fields/src/Controller/GDPRController.php \Drupal\gdpr_fields\Controller\GDPRController::fieldsList()

Lists all fields with GDPR sensitivity.

Return value

array The Views plugins report page.

1 string reference to 'GDPRController::fieldsList'
gdpr_fields.routing.yml in modules/gdpr_fields/gdpr_fields.routing.yml
modules/gdpr_fields/gdpr_fields.routing.yml

File

modules/gdpr_fields/src/Controller/GDPRController.php, line 66

Class

GDPRController
Returns responses for GDPR Field routes.

Namespace

Drupal\gdpr_fields\Controller

Code

public function fieldsList() {
  $filters = GdprFieldFilterForm::getFilters($this->request);
  $output = [];
  $output['filter'] = $this
    ->formBuilder()
    ->getForm(GdprFieldFilterForm::class);
  $output['#attached']['library'][] = 'gdpr_fields/field-list';
  foreach ($this
    ->entityTypeManager()
    ->getDefinitions() as $entityTypeId => $definition) {

    // Skip non-fieldable/config entities.
    if (!$definition
      ->entityClassImplements(FieldableEntityInterface::class)) {
      continue;
    }

    // If a filter is active, exclude any entities that don't match.
    if (!empty($filters['entity_type']) && !in_array($entityTypeId, $filters['entity_type'], FALSE)) {
      continue;
    }
    $bundles = $this->collector
      ->getBundles($entityTypeId);
    $output[$entityTypeId] = [
      '#type' => 'details',
      '#title' => $definition
        ->getLabel() . " [{$entityTypeId}]",
      '#open' => TRUE,
    ];
    if (count($bundles) > 1) {
      $atLeastOneBundleHasFields = FALSE;
      foreach ($bundles as $bundle_id => $bundle_info) {
        $fieldTable = $this
          ->buildFieldTable($definition, $bundle_id, $filters);
        if ($fieldTable) {
          $atLeastOneBundleHasFields = TRUE;
          $output[$entityTypeId][$bundle_id] = [
            '#type' => 'details',
            '#title' => new TranslatableMarkup('%label [%bundle]', [
              '%label' => $bundle_info['label'],
              '%bundle' => $bundle_id,
            ]),
            '#open' => TRUE,
          ];
          $output[$entityTypeId][$bundle_id]['fields'] = $fieldTable;
        }
      }
      if (!$atLeastOneBundleHasFields) {
        unset($output[$entityTypeId]);
      }
    }
    else {

      // Don't add another collapsible wrapper around single bundle entities.
      $bundle_id = $entityTypeId;
      $fieldTable = $this
        ->buildFieldTable($definition, $bundle_id, $filters);
      if ($fieldTable) {
        $output[$entityTypeId][$bundle_id]['fields'] = $fieldTable;
      }
      else {

        // If the entity has no fields because they've been filtered out
        // don't bother including it.
        unset($output[$entityTypeId]);
      }
    }
  }
  return $output;
}