The submission data structure in Webform 4.x has changed, dropping the ['value'] in the array structure. From the webform documentation (Upgrading from Webform 3.x to 4.x):

The $submission->values variable structure changed
In Webform 3.x, values within the Webform $submission variable as retrieved by webform_get_submission() had a structure like the following:

Webform 3.x:

$submission->values[$cid]['value'][0] = 'string value';
However in 4.x, the superflous "value" key was eliminated to increase consistency in Webform's internal data structures during form processing.

Webform 4.x:

$submission->values[$cid][0] = 'string value';

I can create a patch to address this but it sounds like this should belong in a separate branch?

Comments

Roensby’s picture

Suggested implementation for the 4.x branch


/**
 * Implementation of hook_permission().
 */
function webform_encrypt_permission() {
  return array(
    'view encrypted values' => array(
      'title' => t('View Encrypted Values in Webform Results'),
      'description' => t('Users that do not have this permission will see placeholder text.'),
    ),
  );
}

/**
 * Implementation of hook_form_alter()
 */
function webform_encrypt_form_alter(&$form, $form_state, $form_id) {
  // Add our fields to the component add/edit form.
  if ($form_id == 'webform_component_edit_form') {

    $component = $form_state['build_info']['args'][1];

    // Exclude webform component types that don't make sense to encrypt.
    $excluded_types = array('fieldset', 'file', 'markup', 'pagebreak');
    if (!in_array($form['type']['#value'], $excluded_types)) {

      // Add settings for encryption.
      $form['encryption'] = array(
        '#type' => 'fieldset',
        '#title' => t('Encryption'),
        '#tree' => TRUE,
      );
      $form['encryption']['encrypt'] = array(
        '#type' => 'checkbox',
        '#title' => t('Encrypt this field\'s value'),
        '#description' => t('!link to edit encryption settings.', array('!link' => l('Click here', 'admin/config/system/encrypt'))),
        '#default_value' => isset($component['extra']['encrypt']) ? $component['extra']['encrypt'] : 0,
      );
    }

  }
}

/**
 * Implementation of hook_webform_component_presave().
 * Save encryption settings for a component.
 */
function webform_encrypt_webform_component_presave(&$component) {
  if (isset($component['encryption'])) {
    $component['extra'] = array_merge($component['extra'], $component['encryption']);
    unset($component['encryption']);
  }
}

/**
 * Implementation of hook_webform_submission_presave().
 * Encrypt the value if the component has been marked as such.
 */
function webform_encrypt_webform_submission_presave($node, &$submission) {
  foreach ($submission->data as $cid => $entry) {
    if (isset($node->webform['components'][$cid]['extra']['encrypt']) &&
        $node->webform['components'][$cid]['extra']['encrypt']) {
      $submission->data[$cid][0] = encrypt($entry[0], array('base64' => TRUE));
    }
  }
}

/**
 * Implementation of hook_webform_submission_render_alter().
 * Decrypt values when displaying webform submissions.
 */
function webform_encrypt_webform_submission_render_alter(&$renderable) {
  foreach ($renderable['#submission']->data as $cid => $entry) {
    if (isset($renderable['#node']->webform['components'][$cid]['extra']['encrypt']) &&
        $renderable['#node']->webform['components'][$cid]['extra']['encrypt']) {
      $form_key = $renderable['#node']->webform['components'][$cid]['form_key'];
      if (user_access('view encrypted values')) {
        $renderable[$form_key]['#value'] = decrypt($entry[0], array('base64' => TRUE));
      } else {
        $renderable[$form_key]['#value'] = t('[Value Encrypted]');
      }
    }
  }
}

jweowu’s picture

Status: Active » Needs review
StatusFileSize
new1.24 KB

Here's a patch based on the original description. I've ignored #1 as clearly too much has changed since then for non-patch code to be useful.

jcuna’s picture

Issue summary: View changes
StatusFileSize
new3.15 KB

Here's an updated patch that works with webform 7.x-4.2
It's all minor changes to use webform's new structure omitting the [value] from the array.

jcuna’s picture

jcuna’s picture

jcuna’s picture

StatusFileSize
new1.77 KB

Re-uploading patch with diff. Also deleted extra debug line I forgot.

Steven Brown’s picture

@jcuna

I see in your patch there is a function that is called webform_encrypt_webform_submission_load.
This function does not exist in the original 7.x-1.x branch http://cgit.drupalcode.org/webform_encrypt/tree/webform_encrypt.module?h...

If your code is a working version than you are missing code from the last patch.

I did notice that the patch file from comment #3 is not an actual patch and there is data in there that you are referencing in your last patch.

If this is the case then please create a patch that is a diff between 7.x-1.x and your current code that is working. If you need help in doing so please let me know.

I need this for a client and am glad to help.

jcuna’s picture

Hey FatGuyLaughing

My apologies, didn't even remembered that I had patched the module before as I was having issues loading the encrypted data. The patch I applied never made it to the 1.x branch. There was an issue where the decryption was run while a submission was being rendered, as opposed to loading.

Here's the post for that issue.

https://www.drupal.org/node/1433524

jcuna’s picture

nvahalik’s picture

+1 for #2. Requires that you use the AES module (or at least not the default).

sergei_semipiadniy’s picture

StatusFileSize
new2.87 KB

Hi jcuna!
Thanks for your workaround, it helped me a lot. What I did is just remove dpm() and brought better formatting to it.

Again Thanks.

DocVeg’s picture

Why are you not publishing it as a new release? Would be helpful for several people. Best regards!

alexfarr’s picture

Hi,

I had some issues with some of these patches on the 7.x-1 branch. I have created a new patch which includes a check for the webform version so this can be used on both webform 3 and 4. Same changes as #2 but includes a check in the install hook for the webform version number, stores this in a variable and uses that in presave hook to save i the correct format.

alexfarr’s picture

StatusFileSize
new2.49 KB
heddn’s picture

Status: Needs review » Needs work

Re: #13-14
The approach used to determine the webform version isn't recommended. What happens if webforms is updated? This is a perennial issue for drupal modules. The recommended approach is to tie versions of modules to other versions of modules. In this case, tie 1.x of webform_encrypt to 3.x and 2.x to 4.x (for example). It might also be a good time to add support for encrypt 2.x: #2325353: Compatability with Encrypt 7.x-2.x.

alexfarr’s picture

I agree with #15, #14 is not the best approach. A webform encrypt 2.x branch would be the best way forward with this tied to encrypt 2.x and webform 4.x. There are also plenty of other patches floating around that can be included, this is a useful module that needs to be kept up to date.

wavesailor’s picture

How do I get Webform Encrypt to work with the latest Drupal 7 modules?

I am using:
* Drupal 7.43
* Webform 7.x-4.12
* Encrypt 7.x-2.3

If there isn't a released version, then what patches do I need to apply?

thx

ben.hamelin’s picture

spadxiii’s picture

StatusFileSize
new2.46 KB

A re-roll of #14. Though I agree with the version bump to just support webform 4 only.

th_tushar’s picture

Status: Needs work » Fixed

This has been released in 7.x-1.3 version.

th_tushar’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.