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

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.

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);
  }
}

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);
  }
}

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

Thanks, quicksketch!

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

Of course. Will do.

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];
  }
}

StatusFileSize
new2.4 KB

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

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.

Works for me!

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.

StatusFileSize
new1.24 KB

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

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.