You are here

public static function WebformElementEncrypt::processWebformElementEncrypt in Webform Encrypt 8

Processes element attributes.

File

src/Element/WebformElementEncrypt.php, line 40

Class

WebformElementEncrypt
Provides a webform element for element attributes.

Namespace

Drupal\webform_encrypt\Element

Code

public static function processWebformElementEncrypt(&$element, FormStateInterface $form_state, &$complete_form) {
  $webform = $config = $form_state
    ->getFormObject()
    ->getWebform();
  $values = $form_state
    ->getValues();
  $element_name = $values['key'];
  $config = $webform
    ->getThirdPartySetting('webform_encrypt', 'element');
  $encryption_options = \Drupal::service('encrypt.encryption_profile.manager')
    ->getEncryptionProfileNamesAsOptions();
  if (count($encryption_options) > 0) {
    $element['element_encrypt']['encrypt'] = [
      '#type' => 'checkbox',
      '#title' => t("Encrypt this field's value"),
      '#description' => t('<a href=":link">Edit encryption settings.</a>', [
        ':link' => Url::fromRoute('entity.encryption_profile.collection')
          ->toString(),
      ]),
      '#default_value' => isset($config[$element_name]['encrypt']) ? $config[$element_name]['encrypt'] : FALSE,
    ];
    $element['element_encrypt']['encrypt_profile'] = [
      '#type' => 'select',
      '#title' => t('Select Encryption Profile'),
      '#options' => $encryption_options,
      '#default_value' => isset($config[$element_name]['encrypt_profile']) ? $config[$element_name]['encrypt_profile'] : NULL,
      '#states' => [
        'visible' => [
          [
            ':input[name="encrypt"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ],
    ];
    $element['#element_validate'] = [
      [
        get_called_class(),
        'validateWebformElementEncrypt',
      ],
    ];
  }
  else {
    $element['element_encrypt']['message'] = [
      '#markup' => t('Please configure the encryption profile to enable encryption for the element.'),
    ];
  }
  return $element;
}