You are here

gdpr_fields.module in General Data Protection Regulation 8

Module file for the GDPR Fields module.

File

modules/gdpr_fields/gdpr_fields.module
View source
<?php

/**
 * @file
 * Module file for the GDPR Fields module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\gdpr_fields\Entity\GdprField;
use Drupal\gdpr_fields\Entity\GdprFieldConfigEntity;
use Drupal\gdpr_fields\Form\GdprFieldSettingsForm;

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @todo Check user edit permission for GDPR fields.
 */
function gdpr_fields_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /* @var \Drupal\Core\Field\FieldConfigInterface $field */

  // @todo Check that target entity is a content entity.
  $field = $form_state
    ->getFormObject()
    ->getEntity();

  // Do not add GDPR settings to the GDPR Consent Agreement form.
  if ($field
    ->getType() == 'gdpr_user_consent') {
    return;
  }
  $form['field']['gdpr_fields'] = [
    '#type' => 'details',
    '#title' => t('GDPR field settings'),
    '#open' => TRUE,
  ];
  GdprFieldSettingsForm::buildFormFields($form['field']['gdpr_fields'], $field
    ->getTargetEntityTypeId(), $field
    ->getTargetBundle(), $field
    ->getName());
  $form['actions']['submit']['#submit'][] = 'gdpr_fields_form_field_config_edit_form_submit';
}

/**
 * Form submission handler for gdpr_fields_form_field_config_edit_form_alter.
 *
 * This version of the GDPR fields form is embedded on the standard field
 * settings page.
 *
 * @param array $form
 *   The form array.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The form state.
 *
 * @throws \Drupal\Core\Entity\EntityStorageException
 */
function gdpr_fields_form_field_config_edit_form_submit(array $form, FormStateInterface $form_state) {

  /* @var \Drupal\Core\Field\FieldConfigInterface $field */
  $field = $form_state
    ->getFormObject()
    ->getEntity();
  $entity_type = $field
    ->getTargetEntityTypeId();
  $config = GdprFieldConfigEntity::load($entity_type);
  if (NULL === $config) {
    $config = GdprFieldConfigEntity::create([
      'id' => $entity_type,
    ]);
  }
  $field_config = new GdprField([
    'bundle' => $field
      ->getTargetBundle(),
    'name' => $field
      ->getName(),
    'entity_type_id' => $entity_type,
  ]);
  $field_config
    ->setEnabled($form_state
    ->getValue('gdpr_enabled'))
    ->setRta($form_state
    ->getValue('gdpr_rta'))
    ->setRtf($form_state
    ->getValue('gdpr_rtf'))
    ->setAnonymizer($form_state
    ->getValue('gdpr_anonymizer'))
    ->setNotes($form_state
    ->getValue('gdpr_notes'));
  $config
    ->setField($field_config);
  $config
    ->save();
}

Functions

Namesort descending Description
gdpr_fields_form_field_config_edit_form_alter Implements hook_form_FORM_ID_alter().
gdpr_fields_form_field_config_edit_form_submit Form submission handler for gdpr_fields_form_field_config_edit_form_alter.