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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rlhawk’s picture

Status: Active » Needs review
FileSize
1.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

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

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

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

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
FileSize
1.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.

gourav.yadav’s picture

Version: 7.x-3.x-dev » 8.x-5.0-rc3
Category: Feature request » Support request
FileSize
149.59 KB

how to alter exported element's #default-value in webform , i need only 4 columns in my csv file but when load a $form ,i can't seen field
default-value where checked or unchecked.

Aaron23’s picture

Hi Team,

I'm using webform_csv_data_alter and webform_csv_header_alter for modifying my csv file. How can i bring the below structure (In table) in my alter function. All my submission data in one column

Header 1 : WEB
Header 2 : campaign
Header 3 : First Name: Aaron, Second name: Solomon, DOB: 23-10-1991

Thanks,
Aaron