You are here

class GDPRCollector in General Data Protection Regulation 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/gdpr_fields/src/GDPRCollector.php \Drupal\gdpr_fields\GDPRCollector
  2. 8 modules/gdpr_fields/src/GDPRCollector.php \Drupal\gdpr_fields\GDPRCollector

Defines a helper class for stuff related to views data.

Hierarchy

Expanded class hierarchy of GDPRCollector

1 file declares its use of GDPRCollector
GDPRController.php in modules/gdpr_fields/src/Controller/GDPRController.php
1 string reference to 'GDPRCollector'
gdpr_fields.services.yml in modules/gdpr_fields/gdpr_fields.services.yml
modules/gdpr_fields/gdpr_fields.services.yml
1 service uses GDPRCollector
gdpr_fields.collector in modules/gdpr_fields/gdpr_fields.services.yml
Drupal\gdpr_fields\GDPRCollector

File

modules/gdpr_fields/src/GDPRCollector.php, line 17

Namespace

Drupal\gdpr_fields
View source
class GDPRCollector {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * Entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  private $entityFieldManager;

  /**
   * Bundle info.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  private $bundleInfo;

  /**
   * Constructs a GDPRCollector object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
   *   The entity field manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundleInfo
   *   Bundle info.
   */
  public function __construct(EntityTypeManager $entityTypeManager, EntityFieldManagerInterface $entityFieldManager, EntityTypeBundleInfoInterface $bundleInfo) {
    $this->entityTypeManager = $entityTypeManager;
    $this->entityFieldManager = $entityFieldManager;
    $this->bundleInfo = $bundleInfo;
  }

  /**
   * List fields on entity including their GDPR values.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entityType
   *   The entity type id.
   * @param string $bundleId
   *   The entity bundle id.
   * @param array $filters
   *   Array of filters with following keys:
   *   'empty' => filter out entities where all fields are not configured.
   *   'rtf' => only include fields where RTF is configured.
   *   'rta' => only include fields where RTA is configured.
   *   'search' => only include fields whose name match.
   *
   * @return array
   *   GDPR entity field list.
   */
  public function listFields(EntityTypeInterface $entityType, $bundleId, array $filters) {
    $bundleType = $entityType
      ->getBundleEntityType();
    $gdprSettings = GdprFieldConfigEntity::load($entityType
      ->id());

    // @todo explicitly skip commerce_order_item for now as they break bundles
    if ($entityType
      ->id() == 'commerce_order_item') {
      return [];
    }
    $fieldDefinitions = $this->entityFieldManager
      ->getFieldDefinitions($entityType
      ->id(), $bundleId);

    // Get fields for entity.
    $fields = [];

    // If the 'Filter out entities where all fields are not configured' option
    // is set, return an empty array if GDPR is not configured for the entity.
    if ($filters['empty'] && $gdprSettings === NULL) {
      return $fields;
    }
    $hasAtLeastOneConfiguredField = FALSE;
    foreach ($fieldDefinitions as $fieldId => $fieldDefinition) {

      /** @var \Drupal\Core\Field\FieldItemListInterface $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 we're searching by name, check if the label matches search.
      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) {

        /** @var \Drupal\gdpr_fields\Entity\GdprField $field_settings */
        $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;
        }
      }

      // Apply filters.
      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]);
      }
    }

    // Handle the 'Filter out Entities where all fields are not configured'
    // checkbox.
    if ($filters['empty'] && !$hasAtLeastOneConfiguredField) {
      return [];
    }
    return $fields;
  }

  /**
   * Gets bundles belonging to an entity type.
   *
   * @param string $entityType_id
   *   The entity type for which bundles should be located.
   *
   * @return array
   *   Array of bundles.
   */
  public function getBundles($entityType_id) {
    $all_bundles = $this->bundleInfo
      ->getAllBundleInfo();
    $bundles = isset($all_bundles[$entityType_id]) ? $all_bundles[$entityType_id] : [
      $entityType_id => [],
    ];
    return $bundles;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GDPRCollector::$bundleInfo private property Bundle info.
GDPRCollector::$entityFieldManager private property Entity field manager.
GDPRCollector::$entityTypeManager protected property The entity type manager.
GDPRCollector::getBundles public function Gets bundles belonging to an entity type.
GDPRCollector::listFields public function List fields on entity including their GDPR values.
GDPRCollector::__construct public function Constructs a GDPRCollector object.