webform_encrypt.module 2.57 KB
Newer Older
Jake Bell's avatar
Jake Bell committed
1 2
<?php

Jake Bell's avatar
Jake Bell committed
3 4 5 6 7
/**
 * @file
 * Main module file for the Webform Encrypt module.
 */

th_tushar's avatar
th_tushar committed
8
module_load_include('inc', 'webform_encrypt', 'includes/webform_encrypt.theme');
9 10 11 12

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
th_tushar's avatar
th_tushar committed
13 14 15 16
function webform_encrypt_form_webform_ui_element_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $webform = $form_state->getBuildInfo()['args']['0'];
  $element_id = $form_state->getBuildInfo()['args']['1'];
  $webform_elements = $webform->getElementsInitializedAndFlattened();
17

th_tushar's avatar
th_tushar committed
18 19 20 21 22 23 24
  $allowed_types = array('textfield', 'textarea');
  if (in_array($webform_elements[$element_id]['#type'], $allowed_types)) {
    // To provide the fieldset for encryption fields on element config form.
    $form['element_encrypt'] = [
      '#type' => 'details',
      '#title' => t('Encryption'),
    ];
25

th_tushar's avatar
th_tushar committed
26 27 28
    // To provide the encryption fields for webform elements.
    $form['element_encrypt']['encrypt'] = [
      '#type' => 'webform_element_encrypt',
29
      '#title' => t('Encryption'),
th_tushar's avatar
th_tushar committed
30 31
      '#title_display' => 'hidden',
    ];
Jake Bell's avatar
Jake Bell committed
32
  }
33
}
Jake Bell's avatar
Jake Bell committed
34

35 36 37 38
/**
 * Implementation of hook_form_alter().
 */
function webform_encrypt_form_alter(&$form, &$form_state, $form_id) {
th_tushar's avatar
th_tushar committed
39 40 41 42
  // When we are editing a webform submission,
  if (strpos($form_id, 'webform_submission_') === 0) {
    $config = \Drupal::service('config.factory')->get('webform.encrypt')->get('element.settings');
    $elements = $form['elements'];
Jake Bell's avatar
Jake Bell committed
43

th_tushar's avatar
th_tushar committed
44 45 46 47
    foreach ($elements as $key => &$element) {
      if (isset($config[$key]['encrypt']) && $config[$key]['encrypt']) {
        $encryption_profile = \Drupal\encrypt\Entity\EncryptionProfile::load($config[$key]['encrypt_profile']);
        $form['elements'][$key]['#default_value'] = Drupal::service('encryption')->decrypt($element['#default_value'], $encryption_profile);
48
      }
Jake Bell's avatar
Jake Bell committed
49 50 51 52 53
    }
  }
}

/**
th_tushar's avatar
th_tushar committed
54
 * Implements hook_entity_presave().
55
 *
th_tushar's avatar
th_tushar committed
56
 * @param \Drupal\Core\Entity\EntityInterface $entity
57
 */
th_tushar's avatar
th_tushar committed
58 59 60 61
function webform_encrypt_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity instanceof Drupal\webform\Entity\WebformSubmission) {
    $config = \Drupal::service('config.factory')->get('webform.encrypt')->get('element.settings');
    $data_original = $entity->getData();
62

th_tushar's avatar
th_tushar committed
63 64 65 66
    foreach ($data_original as $key => $value) {
      if (isset($config[$key]['encrypt']) && $config[$key]['encrypt']) {
        $encryption_profile = \Drupal\encrypt\Entity\EncryptionProfile::load($config[$key]['encrypt_profile']);
        $data[$key] = Drupal::service('encryption')->encrypt($value, $encryption_profile);
67 68
      }
      else {
th_tushar's avatar
th_tushar committed
69
        $data[$key] = $value;
70
      }
71
    }
th_tushar's avatar
th_tushar committed
72
    $entity->setData($data);
73 74
  }
}