hook_webform_component_display_alter is great for modifying data when displaying submitted webform results. It would be useful to have a similar hook for exported data.

Comments

rlhawk’s picture

Status:Active» Needs review
StatusFileSize
new1.45 KB

The attached patch adds two hooks: one to alter the CSV header and one to alter the CSV data.

rlhawk’s picture

Examples of how this could be implemented:

// Uses the machine name for component headers, but only for the webform with node 5 and components that are text fields
function foo_webform_csv_header_alter(&$header, $component) {
  if ($component['nid'] == 5 && $component['type'] == 'textfield') {
    $header[2] = $component['form_key'];
  }
}

// Make a string substitution for all email fields
function foo_webform_csv_data_alter(&$data, $component) {
  if ($component['type'] == 'email') {
    $data = str_replace('foo', 'bar', $data);
  }
}

rlhawk’s picture

Or within a module like Webform Localization to allow exported data to be translated. For instance:

function webform_localization_webform_csv_data_alter(&$data, $component) {
  // Gets webform localization options that match this node ID.
  $wl_options = webform_localization_get_config($component['nid']);
  // Translate the translatable properties.
  if ($wl_options['expose_strings']) {
    module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
    _webform_localization_translate_component($data, $component);
  }
}
quicksketch’s picture

Sounds like a great idea to me. I'll review this when I have a chance.

rlhawk’s picture

Thanks, quicksketch!

quicksketch’s picture

If you can also include documentation in webform.api.php for these new hooks, that would be great and move this along faster.

rlhawk’s picture

Of course. Will do.

rlhawk’s picture

StatusFileSize
new2.4 KB

Here's a new patch that includes documentation in webform.api.php for the two export hooks. I also added $submission as a parameter to the data hook, so that fields can be altered based on other fields in the submission.

Here's the example I used in the API file:

function hook_webform_csv_data_alter(&$data, $component, $submission) {
  // If a value of a field was left blank, use the value from another
  // field.
  if ($component['cid'] == 1 && empty($data)) {
    $data = $submission->data[2]['value'][0];
  }
}
rlhawk’s picture

StatusFileSize
new2.4 KB

The API examples had some dumb errors, which I've fixed. Replacement patch attached.

tim.plunkett’s picture

Status:Needs review» Reviewed & tested by the community

This works great and the documentation is there. Is 7.x-3.x getting new features still, this would be nice to have.

pfournier’s picture

Works for me!

rahulbile’s picture

StatusFileSize
new1.25 KB

I wanted to remove and adjust the first two headers values also, so have modified patch a bit, if someone wants to do the same, can use this.

rahulbile’s picture

StatusFileSize
new1.24 KB

Wrong patch attached above. correct patch for changing first two headers values headers if needed.

quicksketch’s picture

Status:Reviewed & tested by the community» Fixed
StatusFileSize
new1.98 KB

Thanks guys, I moved the location of the drupal_alter() call as @rahulbile demonstrated, but kept all the documentation from @rlhawk's patch. Although 3.x is not receiving new features in most cases, this minor change doesn't require any changes for 3.x, so I've committed it to all branches 3.x and 4.x.

Status:Fixed» Closed (fixed)

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

j_ten_man’s picture

Issue summary:View changes
Status:Closed (fixed)» Needs work

I'm surprised no one has said anything, but you committed the wrong patch. You committed #12, but #13 is what you should have committed.

DanChadwick’s picture

Status:Needs work» Closed (fixed)

I don't think so. This has been revised in the 7.x-4.x branch. In that branch, the drupal_alter of webform_csv_header and webform_csv_data allow the component's header and data to be altered, not the row's. There are numerous other alters. I haven't compared this to the -3.x branch, but what I see is proper for 4.x and it would appear from #14 that -3.x is consistent.

If you review the branches and believe I am in error, please do re-open and explain. I only looked at this code briefly.

j_ten_man’s picture

7.x-3.x branch is correct, but 7.x-3.20 is incorrect. I'm not sure when you made the change to dev portion of this, but that's why I'm seeing the issue. I'll just modify my 7.x-3.20 to match the dev branch.

DanChadwick’s picture

I'd recommend you just use the dev in that case, rather than patching 3.20. Or maybe it's time for another 3.x release.