You are here

public static function GdprFieldSettingsForm::buildFormFields in General Data Protection Regulation 8

Same name and namespace in other branches
  1. 8.2 modules/gdpr_fields/src/Form/GdprFieldSettingsForm.php \Drupal\gdpr_fields\Form\GdprFieldSettingsForm::buildFormFields()
  2. 3.0.x modules/gdpr_fields/src/Form/GdprFieldSettingsForm.php \Drupal\gdpr_fields\Form\GdprFieldSettingsForm::buildFormFields()

Builds the form fields for GDPR settings.

This is in a separate method so it can also be attached to the regular field settings page by hook.

Parameters

array $form: Form.

string $entity_type: Entity type.

string $bundle_name: Bundle.

string $field_name: Field.

Return value

array Form.

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

See also

gdpr_fields_form_field_config_edit_form_submit

2 calls to GdprFieldSettingsForm::buildFormFields()
GdprFieldSettingsForm::buildForm in modules/gdpr_fields/src/Form/GdprFieldSettingsForm.php
Form constructor.
gdpr_fields_form_field_config_edit_form_alter in modules/gdpr_fields/gdpr_fields.module
Implements hook_form_FORM_ID_alter().

File

modules/gdpr_fields/src/Form/GdprFieldSettingsForm.php, line 243

Class

GdprFieldSettingsForm
GDPR Field settings.

Namespace

Drupal\gdpr_fields\Form

Code

public static function buildFormFields(array &$form, $entity_type = NULL, $bundle_name = NULL, $field_name = NULL) {
  $config = static::getConfig($entity_type, $bundle_name, $field_name);

  /* @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */

  /* @var \Drupal\anonymizer\Anonymizer\AnonymizerFactory $anonymizer_factory */
  $entity_type_manager = \Drupal::entityTypeManager();
  $field_manager = \Drupal::service('entity_field.manager');
  $anonymizer_factory = \Drupal::service('anonymizer.anonymizer_factory');
  $anonymizer_definitions = $anonymizer_factory
    ->getDefinitions();
  $entity_definition = $entity_type_manager
    ->getDefinition($entity_type);
  $field_definition = $field_manager
    ->getFieldDefinitions($entity_type, $bundle_name)[$field_name];

  // Exclude uuid/bundle.
  if ($entity_definition
    ->getKey('uuid') == $field_name || $entity_definition
    ->getKey('bundle') == $field_name) {
    return [];
  }
  $form['gdpr_enabled'] = [
    '#type' => 'checkbox',
    '#title' => t('This is a GDPR field'),
    '#default_value' => $config->enabled,
  ];
  $form['gdpr_relationship'] = [
    '#type' => 'value',
    '#value' => GdprField::RELATIONSHIP_DISABLED,
  ];
  $form['gdpr_sars_filename'] = [
    '#type' => 'value',
    '#value' => $config->sarsFilename,
  ];
  if ($field_definition
    ->getType() == 'entity_reference') {
    $inner_entity_type = $field_definition
      ->getSetting('target_type');
    $inner_entity_definition = $entity_type_manager
      ->getDefinition($inner_entity_type);
    $form['gdpr_relationship'] = [
      '#type' => 'select',
      '#default_value' => $config->relationship,
      '#options' => [
        GdprField::RELATIONSHIP_DISABLED => new TranslatableMarkup('Do not follow this relationship.'),
        GdprField::RELATIONSHIP_FOLLOW => new TranslatableMarkup('This %entity_type_label owns the referenced %target_entity_type_label (Relationship will be followed)', [
          '%entity_type_label' => $entity_definition
            ->getLabel(),
          '%target_entity_type_label' => $inner_entity_definition
            ->getLabel(),
        ]),
        GdprField::RELATIONSHIP_OWNER => new TranslatableMarkup('This %entity_type_label is owned by the referenced %target_entity_type_label', [
          '%entity_type_label' => $entity_definition
            ->getLabel(),
          '%target_entity_type_label' => $inner_entity_definition
            ->getLabel(),
        ]),
      ],
      '#title' => t('Relationship Handling'),
      '#description' => new TranslatableMarkup('Owned entities are included in any task which contains the owner.', [
        '%type' => $inner_entity_definition
          ->getLabel(),
      ]),
      '#states' => [
        'visible' => [
          ':input[name="gdpr_enabled"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  $form['gdpr_rta'] = [
    '#type' => 'select',
    '#weight' => 10,
    '#title' => t('Right to access'),
    '#options' => GdprField::rtaOptions(),
    '#default_value' => $config->rta,
    '#states' => [
      'visible' => [
        ':input[name="gdpr_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['gdpr_rtf'] = [
    '#weight' => 20,
    '#type' => 'select',
    '#title' => t('Right to be forgotten'),
    '#options' => GdprField::rtfOptions(),
    '#default_value' => $config->rtf,
    '#states' => [
      'visible' => [
        ':input[name="gdpr_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $error_message = NULL;
  if ($entity_definition
    ->getKey('id') == $field_name) {

    // If this is the entity's ID, treat the removal as remove the entire
    // entity.
    unset($form['gdpr_rtf']['#options']['anonymise']);
    $form['gdpr_rtf']['#options']['remove'] = new TranslatableMarkup('Delete entire entity');

    // Define target filename for this bundle.
    // @todo: Move to a form alter in gdpr_tasks.
    // @todo: Add <inherit> option to inherit owned entity filename.
    $form['gdpr_sars_filename'] = [
      '#type' => 'textfield',
      '#title' => t('Right to access filename'),
      '#description' => t('Specify the filename for the owned entity to go in. Use %inherit to keep the related entity in the same file.', []),
      // Default to the entity type.
      '#default_value' => $config->sarsFilename,
      '#field_suffix' => '.csv',
      '#size' => 20,
      // Between RTA and RTF.
      '#weight' => 15,
      '#required' => TRUE,
      '#states' => [
        'visible' => [
          ':input[name="gdpr_enabled"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  elseif (!$config
    ->propertyCanBeRemoved($field_definition, $error_message)) {
    unset($form['gdpr_rtf']['#options']['remove']);
    $form['gdpr_rtf_disabled'] = [
      '#type' => 'item',
      '#markup' => new TranslatableMarkup('This field cannot be removed, only anonymised.'),
      '#description' => $error_message,
    ];
  }

  // Force removal to 'no' for computed properties.
  if ($field_definition
    ->isComputed()) {
    $form['gdpr_rtf']['#default_value'] = 'no';
    $form['gdpr_rtf']['#disabled'] = TRUE;
    $form['gdpr_rtf']['#description'] = t('*This is a computed field and cannot be removed.');
  }
  $sanitizer_options = array_map(function ($s) {
    return $s['label'];
  }, $anonymizer_definitions);
  $form['gdpr_anonymizer'] = [
    '#weight' => 30,
    '#type' => 'select',
    '#title' => t('Anonymizer to use'),
    '#options' => $sanitizer_options,
    '#default_value' => $config->anonymizer,
    '#states' => [
      'visible' => [
        ':input[name="gdpr_enabled"]' => [
          'checked' => TRUE,
        ],
        ':input[name="gdpr_rtf"]' => [
          'value' => 'anonymize',
        ],
      ],
      'required' => [
        ':input[name="gdpr_enabled"]' => [
          'checked' => TRUE,
        ],
        ':input[name="gdpr_rtf"]' => [
          'value' => 'anonymize',
        ],
      ],
    ],
  ];
  $form['gdpr_notes'] = [
    '#weight' => 40,
    '#type' => 'textarea',
    '#title' => 'Notes',
    '#default_value' => $config->notes,
    '#states' => [
      'visible' => [
        ':input[name="gdpr_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
}