You are here

class GDPRController in General Data Protection Regulation 3.0.x

Same name in this branch
  1. 3.0.x modules/gdpr_fields/src/Controller/GDPRController.php \Drupal\gdpr_fields\Controller\GDPRController
  2. 3.0.x modules/gdpr_tasks/src/Controller/GDPRController.php \Drupal\gdpr_tasks\Controller\GDPRController
Same name and namespace in other branches
  1. 8.2 modules/gdpr_fields/src/Controller/GDPRController.php \Drupal\gdpr_fields\Controller\GDPRController
  2. 8 modules/gdpr_fields/src/Controller/GDPRController.php \Drupal\gdpr_fields\Controller\GDPRController

Returns responses for GDPR Field routes.

Hierarchy

Expanded class hierarchy of GDPRController

File

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

Namespace

Drupal\gdpr_fields\Controller
View source
class GDPRController extends ControllerBase {

  /**
   * Stores the Views data cache object.
   *
   * @var \Drupal\gdpr_fields\GDPRCollector
   */
  protected $collector;

  /**
   * Current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * Constructs a new GDPRController.
   *
   * @param \Drupal\gdpr_fields\GDPRCollector $collector
   *   The GDPR collector service.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   HTTP Request stack.
   */
  public function __construct(GDPRCollector $collector, RequestStack $request_stack) {
    $this->collector = $collector;
    $this->request = $request_stack
      ->getCurrentRequest();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('gdpr_fields.collector'), $container
      ->get('request_stack'));
  }

  /**
   * Lists all fields with GDPR sensitivity.
   *
   * @return array
   *   The Views plugins report page.
   */
  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;
  }

  /**
   * Build a table for entity field list.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type id.
   * @param string $bundle_id
   *   The entity bundle id.
   * @param array $filters
   *   Filters.
   *
   * @return array
   *   Renderable array for field list table.
   */
  protected function buildFieldTable(EntityTypeInterface $entity_type, $bundle_id, array $filters) {
    $rows = $this->collector
      ->listFields($entity_type, $bundle_id, $filters);
    if (count($rows) === 0) {
      return NULL;
    }

    // Sort rows by field name.
    ksort($rows);
    $table = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Name'),
        $this
          ->t('Type'),
        $this
          ->t('Right to access'),
        $this
          ->t('Right to be forgotten'),
        $this
          ->t('Notes'),
        '',
      ],
      '#sticky' => TRUE,
    ];
    $delta = 0;
    foreach ($rows as $row) {
      $table[$delta]['title'] = [
        '#plain_text' => $row['title'],
      ];
      $type_markup = $row['is_id'] || $row['type'] === 'entity_reference' ? "<strong>{$row['type']}</strong>" : $row['type'];
      $table[$delta]['type'] = [
        '#markup' => new FormattableMarkup($type_markup, []),
      ];
      $table[$delta]['rta'] = [
        '#plain_text' => $row['rta'],
      ];
      $table[$delta]['rtf'] = [
        '#plain_text' => $row['rtf'],
      ];
      $table[$delta]['notes'] = [
        '#markup' => empty($row['notes']) ? '' : '<span class="notes" data-icon="?"></span><div>' . $row['notes'] . '</div>',
      ];
      $table[$delta]['edit'] = [
        '#markup' => !empty($row['edit']) ? $row['edit']
          ->toString() : '',
      ];
      ++$delta;
    }
    return $table;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
GDPRController::$collector protected property Stores the Views data cache object.
GDPRController::$request protected property Current request.
GDPRController::buildFieldTable protected function Build a table for entity field list.
GDPRController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
GDPRController::fieldsList public function Lists all fields with GDPR sensitivity.
GDPRController::__construct public function Constructs a new GDPRController.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.