You are here

function field_encrypt_form_alter in Field Encryption 3.0.x

Same name and namespace in other branches
  1. 8.2 field_encrypt.module \field_encrypt_form_alter()

Implements hook_form_alter().

Adds settings to the field storage configuration forms to allow setting the encryption state.

File

./field_encrypt.module, line 25
Contains module hooks for field_encrypt.

Code

function field_encrypt_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // If this is the add or edit form for field_storage, we call our function.
  if ($form_id === 'field_storage_add_form' || $form_id === 'field_storage_config_edit_form') {

    // Check permissions.
    $user = \Drupal::currentUser();
    if ($user
      ->hasPermission('administer field encryption') && \Drupal::config('field_encrypt.settings')
      ->get('encryption_profile') !== '') {

      /** @var \Drupal\field\Entity\FieldStorageConfig $field */
      $field = $form_state
        ->getFormObject()
        ->getEntity();
      $field_type = $field
        ->getType();
      $default_properties = \Drupal::config('field_encrypt.settings')
        ->get('default_properties');

      // Add container for field_encrypt specific settings.
      $form['field_encrypt'] = [
        '#type' => 'details',
        '#title' => t('Field encryption'),
        '#open' => TRUE,
      ];

      // Display a warning about changing field data.
      if ($form_id == "field_storage_config_edit_form" && $field
        ->hasData()) {
        $form['field_encrypt']['#prefix'] = '<div class="messages messages--warning">' . t('Warning: changing field encryption settings may cause data corruption!<br />When changing these settings, existing fields will be (re)encrypted in batch according to the new settings. <br />Make sure you have a proper backup, and do not perform this action in an environment where the data will be changing during the batch operation, to avoid data loss.') . '</div>';
      }
      $form['field_encrypt']['field_encrypt'] = [
        '#type' => 'container',
        '#tree' => TRUE,
      ];

      // Add setting to decide if field should be encrypted.
      $form['field_encrypt']['field_encrypt']['encrypt'] = [
        '#type' => 'checkbox',
        '#title' => t('Encrypt field'),
        '#description' => t('Makes the field storage encrypted.'),
        '#default_value' => $field
          ->getThirdPartySetting('field_encrypt', 'encrypt', FALSE),
      ];
      $properties = [];
      $definitions = $field
        ->getPropertyDefinitions();
      foreach ($definitions as $property => $definition) {
        $properties[$property] = $definition
          ->getLabel();
      }
      $field_encrypt_default = isset($default_properties[$field_type]) ? $default_properties[$field_type] : [];
      $form['field_encrypt']['field_encrypt']['properties'] = [
        '#type' => 'checkboxes',
        '#title' => t('Properties'),
        '#description' => t('Specify the field properties to encrypt.'),
        '#options' => $properties,
        '#default_value' => $field
          ->getThirdPartySetting('field_encrypt', 'properties', $field_encrypt_default),
        '#states' => [
          'visible' => [
            ':input[name="field_encrypt[encrypt]"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];

      // We add functions to process the form when it is saved.
      $form['#entity_builders'][] = 'field_encrypt_form_field_add_form_builder';
    }
  }
}