webform_encrypt.module 2.68 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.
 */

8 9 10 11 12
use Drupal\Core\Form\FormStateInterface;
use Drupal\encrypt\Entity\EncryptionProfile;
use Drupal\Core\Entity\EntityInterface;
use Drupal\webform\Entity\WebformSubmission;

th_tushar's avatar
th_tushar committed
13
module_load_include('inc', 'webform_encrypt', 'includes/webform_encrypt.theme');
14 15 16 17

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
18
function webform_encrypt_form_webform_ui_element_form_alter(&$form, FormStateInterface $form_state) {
th_tushar's avatar
th_tushar committed
19 20 21
  $webform = $form_state->getBuildInfo()['args']['0'];
  $element_id = $form_state->getBuildInfo()['args']['1'];
  $webform_elements = $webform->getElementsInitializedAndFlattened();
22

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

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

40 41 42 43
/**
 * Implementation of hook_form_alter().
 */
function webform_encrypt_form_alter(&$form, &$form_state, $form_id) {
44
  // When we are editing a webform submission.
th_tushar's avatar
th_tushar committed
45 46 47
  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
48

th_tushar's avatar
th_tushar committed
49 50
    foreach ($elements as $key => &$element) {
      if (isset($config[$key]['encrypt']) && $config[$key]['encrypt']) {
51
        $encryption_profile = EncryptionProfile::load($config[$key]['encrypt_profile']);
th_tushar's avatar
th_tushar committed
52
        $form['elements'][$key]['#default_value'] = Drupal::service('encryption')->decrypt($element['#default_value'], $encryption_profile);
53
      }
Jake Bell's avatar
Jake Bell committed
54 55 56 57 58
    }
  }
}

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

th_tushar's avatar
th_tushar committed
68 69
    foreach ($data_original as $key => $value) {
      if (isset($config[$key]['encrypt']) && $config[$key]['encrypt']) {
70
        $encryption_profile = EncryptionProfile::load($config[$key]['encrypt_profile']);
th_tushar's avatar
th_tushar committed
71
        $data[$key] = Drupal::service('encryption')->encrypt($value, $encryption_profile);
72 73
      }
      else {
th_tushar's avatar
th_tushar committed
74
        $data[$key] = $value;
75
      }
76
    }
th_tushar's avatar
th_tushar committed
77
    $entity->setData($data);
78 79
  }
}