You are here

public function EncryptionProfileForm::form in Encrypt 8.3

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/EncryptionProfileForm.php, line 98

Class

EncryptionProfileForm
Provides the form to add / edit an EncryptionProfile entity.

Namespace

Drupal\encrypt\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /* @var $encryption_profile \Drupal\encrypt\Entity\EncryptionProfile */
  $encryption_profile = $this->entity;

  // If the profile is being edited and editing has not been confirmed yet,
  // display a warning and require confirmation.
  if ($this->operation == "edit" && !$this->editConfirmed) {
    $form['confirm_edit'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('Be extremely careful when editing an encryption profile! It may result in making data encrypted with this profile unreadable. Are you sure you want to edit this profile?'),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    ];
    return $form;
  }

  // If editing has been confirmed, display the edit form.
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $encryption_profile
      ->label(),
    '#description' => $this
      ->t("Label for the encryption profile."),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $encryption_profile
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\encrypt\\Entity\\EncryptionProfile::load',
    ],
    '#disabled' => !$encryption_profile
      ->isNew(),
  ];

  // This is the element that contains all of the dynamic parts of the form.
  $form['encryption'] = [
    '#type' => 'container',
    '#prefix' => '<div id="encrypt-settings">',
    '#suffix' => '</div>',
  ];
  $encryption_methods = $this->encryptService
    ->loadEncryptionMethods(FALSE);
  $method_options = [];

  // Show the current encryption plugin, even if deprecated.
  if (!$encryption_profile
    ->isNew()) {
    $method = $encryption_profile
      ->getEncryptionMethod();
    $method_options[$method
      ->getPluginId()] = $method
      ->getLabel();
  }
  foreach ($encryption_methods as $plugin_id => $definition) {
    $method_options[$plugin_id] = (string) $definition['title'];
  }
  $form['encryption']['encryption_method'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Encryption Method'),
    '#description' => $this
      ->t('Select the method used for encryption'),
    '#options' => $method_options,
    '#required' => TRUE,
    '#default_value' => $encryption_profile
      ->getEncryptionMethodId(),
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxUpdateSettings',
      ],
      'event' => 'change',
      'wrapper' => 'encrypt-settings',
    ],
  ];
  $form['encryption']['encryption_method_configuration'] = [
    '#type' => 'container',
    '#title' => $this
      ->t('Encryption method settings'),
    '#title_display' => FALSE,
    '#tree' => TRUE,
  ];
  if ($encryption_profile
    ->getEncryptionMethod() instanceof EncryptionMethodPluginFormInterface) {
    $plugin_form_state = $this
      ->createPluginFormState($form_state);
    $form['encryption']['encryption_method_configuration'] += $encryption_profile
      ->getEncryptionMethod()
      ->buildConfigurationForm([], $plugin_form_state);
    $form_state
      ->setValue('encryption_method_configuration', $plugin_form_state
      ->getValues());
  }
  $form['encryption']['encryption_key'] = [
    '#type' => 'key_select',
    '#title' => $this
      ->t('Encryption Key'),
    '#required' => TRUE,
    '#default_value' => $encryption_profile
      ->getEncryptionKeyId(),
  ];

  // Filter the list of available keys by the "encryption" key type group.
  $key_filters = [
    'type_group' => 'encryption',
  ];
  $form['encryption']['encryption_key']['#key_filters'] = $key_filters;
  return $form;
}