View source
<?php
namespace Drupal\gdpr_fields\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\gdpr_fields\Entity\GdprField;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class GdprFieldFilterForm extends FormBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function getFormId() {
return 'gdpr_fields_field_filter_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$filters = self::getFilters($this
->getRequest());
$form['container'] = [];
$entities = [];
foreach ($this->entityTypeManager
->getDefinitions() as $key => $definition) {
if ($definition
->entityClassImplements(FieldableEntityInterface::class)) {
$entities[$key] = $definition
->getLabel();
}
}
$form['container']['search'] = [
'#type' => 'textfield',
'#title' => $this
->t('Search'),
'#default_value' => $filters['search'],
];
$form['container']['entity_type'] = [
'#type' => 'select',
'#title' => $this
->t('Entity Type'),
'#options' => $entities,
'#multiple' => TRUE,
'#default_value' => $filters['entity_type'],
];
$form['container']['rta'] = [
'#type' => 'select',
'#title' => $this
->t('Right to access'),
'#options' => [
'0' => 'Not configured',
] + GdprField::rtaOptions(),
'#multiple' => TRUE,
'#default_value' => $filters['rta'],
];
$form['container']['rtf'] = [
'#type' => 'select',
'#title' => $this
->t('Right to be forgotten'),
'#options' => [
'0' => 'Not configured',
] + GdprField::rtfOptions(),
'#multiple' => TRUE,
'#default_value' => $filters['rtf'],
];
$form['container']['empty'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Filter out Entities where all fields are not configured'),
'#default_value' => $filters['empty'],
];
$form['container']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Apply'),
'#button_type' => 'primary',
];
$form['container']['reset'] = [
'#type' => 'submit',
'#value' => $this
->t('Reset'),
'#submit' => [
[
$this,
'resetForm',
],
],
'#limit_validation_errors' => [],
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$arguments = [
'search' => $form_state
->getValue('search'),
'entity_type' => $form_state
->getValue('entity_type'),
'rta' => $form_state
->getValue('rta'),
'rtf' => $form_state
->getValue('rtf'),
'empty' => $form_state
->getValue('empty'),
];
$form_state
->setRedirect('gdpr_fields.fields_list', [], [
'query' => $arguments,
]);
}
public function resetForm(array &$form, FormStateInterface $form_state) {
$form_state
->setRedirect('gdpr_fields.fields_list');
}
public static function getFilters(Request $request) {
$filters = [
'search' => $request
->get('search'),
'entity_type' => $request
->get('entity_type'),
'rta' => $request
->get('rta'),
'rtf' => $request
->get('rtf'),
'empty' => $request
->get('empty'),
];
if ($filters['rtf'] === NULL) {
$filters['rtf'] = [];
}
if ($filters['rta'] === NULL) {
$filters['rta'] = [];
}
return $filters;
}
}