I'm looking for a way to prepopulate a form component with one or more values that were filled out in one of its node fields.

To be more precise:

  1. A user creates node with a webform-enabled content type
  2. In the node he fills out two 2 dates on which an event takes place (with a Date field - multiple values)
  3. In the webform of that node these dates should appear as selectable options (the visitor can choose on which date of those two he wants to show up on the event)

I'm aware of the fact that this may be out of scope of the issue queue, but if this is currently not provided by the Webform module (because it's too custom), would there be a (related) module or method to achieve this?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

I actually think this is possible... but only in the 4.x branch because it has full token support. To do this, set up a select list with the two options:

0|[node:field_date:delta:0]
1|[node:field_date:delta:1]

I think that would display the first field value as the label of the first choice and the second field value as the second choice, but I'm not 100% sure you can do that, accessing individual delta values. You definitely could do it if the two date fields were separate. Unfortunately this definitely won't work in the 3.x branch. The only option there is to use hook_form_alter() to inject the choices, but that generally won't have an effect on the sent e-mails, viewing submissions, etc. so it's not a real good option.

knalstaaf’s picture

Version: 7.x-3.x-dev » 7.x-4.x-dev
FileSize
13.72 KB
84.86 KB

I'm now trying it on a local 7.x-4.x-dev installation and the Date field tokens are indeed available. I can't get it to work though (used tokens, tried the ones you proposed as well). Both the options are available for the visitor but they seem to have no label (I'm just seeing 2 radio buttons).

quicksketch’s picture

Title: Default value(s) in form component from Webform-enabled content type field » Node tokens should be available in select component option labels
Category: support » bug

Hi @knalstaaf, thanks for trying out the new version. So I think this *should* work, but after looking at the code I can tell why it's not. The _webform_select_options_from_text() function is where token-replacement happens, but it doesn't have access to the $node object, which is necessary to replace the field tokens.

To fix this problem, I think we should separate the token replacement from the _webform_select_options_from_text() function, and run webform_token_replace() on each of the labels. I'm moving this to a bug, since at least we shouldn't be saying you can use node tokens in the dialog browser if it's not really available.

thechanceg’s picture

Here is my go at a patch for this.

I just did what you suggested quicksketch. Moved webform_replace_tokens from _webform_select_options_from_text() to _webform_render_select().

thechanceg’s picture

Status: Active » Needs review
quicksketch’s picture

Thanks @thechanceg! This looks great, exactly what I had in mind.

The only problem here is that we should wrap the token replacement here:

+  foreach ($options as $key => $option) {
+    $options[$key] = webform_replace_tokens($option, $node);
+  }

There should be an if ($filter) { } check around that. I know the $filter parameter isn't well-understood, but it's used when we want to display the element or results with the raw tokens in place instead of being replaced (e.g. in form builder or on a settings page).

thechanceg’s picture

Thanks for the clarification @quicksketch. I can see how that would be necessary.

Here is a revised patch. Now with 100% fewer spelling errors in the description!

jweowu’s picture

Status: Needs review » Reviewed & tested by the community

The patch works for me. The code looks sane, and has quicksketch's prior approval for the approach, so I think RTBC.

quicksketch’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
11.42 KB

This patch still needed work because it didn't take into account any of the following:

- Because the $filter option is no longer used in several functions, it should be removed from the argument list.
- Because $filter isn't used in option callbacks any more, it should be removed from the webform.api.php documentation as well.
- The patch only affected node tokens when rendering the form, but not in table, analysis, or csv downloads.
- We didn't take into account Grid components which use the same callbacks for generating options lists.

I *think* this version addresses all these problems, but we should confirm that node token replacement is working in all the places we expect:

- Rendering the form.
- Rendering results and e-mails.
- Analysis, table, and CSV downloads.
- And all of the above for grid components in addition to select components.

jweowu’s picture

Issue summary: View changes
Status: Needs review » Needs work

There's at least one problem with this patch: It removes the $filter argument from _webform_select_options_callback(), but that function still tries to use it:

return $function($component, $flat, $filter, $arguments);

That function variable comes from $info[$name]['options callback'], and grepping for 'options callback' gives the following:

./includes/webform.options.inc:18:    'options callback' => 'webform_options_days',
./includes/webform.options.inc:24:    'options callback' => 'webform_options_countries',
./includes/webform.options.inc:30:    'options callback' => 'webform_options_united_states',

Each of those functions takes a $filter argument, but none of them uses it in any way.

quicksketch’s picture

Status: Needs work » Needs review
FileSize
12.95 KB

Thanks @jweowu for the feedback! You're right on all counts. I've rerolled to fix these issues. Great catches.

quicksketch’s picture

Status: Needs review » Fixed

Finally pushed this into the 7.x-4.x branch. Thanks everyone here for their work on this one.

knalstaaf’s picture

Cool, thanks!

Throughout my five year stay on the Drupal community it occurred to me that you're one of the most dedicated maintainers. I'm convinced most of the maintainers are as dedicated as their schedule allows them to, but yours is rather exceptional, as well as in general feedback on issues and support as in developing, innovating and perfectioning modules.

It doesn't go unnoticed. Thanks for all the effort so far!

Status: Fixed » Closed (fixed)

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

sunshinee’s picture

Leaving this as fixed, but want to add a note in case it will help others.

If you are using the Values module to provide select option lists, this update breaks the functionality and returns an error "Missing argument 4 for values_webform_webform_select_options()."

There is an issue filed in the Values issue queue: https://drupal.org/node/2242571

Joy

fenstrat’s picture

Version: 7.x-4.x-dev » 8.x-4.x-dev
Assigned: Unassigned » fenstrat
Status: Closed (fixed) » Patch (to be ported)

Needs porting to 8.x-4.x.

fenstrat’s picture

Version: 8.x-4.x-dev » 7.x-4.x-dev
Assigned: fenstrat » Unassigned
Status: Patch (to be ported) » Fixed

Committed and pushed 0838ce1 to 8.x-4.x. Thanks!

  • Commit 6fff0ac on 8.x-4.x by fenstrat:
    Issue #2064851 by thechanceg, jweowu, and quicksketch: Node tokens...

Status: Fixed » Closed (fixed)

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