Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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:
- A user creates node with a webform-enabled content type
- In the node he fills out two 2 dates on which an event takes place (with a Date field - multiple values)
- 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?
Comment | File | Size | Author |
---|---|---|---|
#11 | webform-token-replacement-2064851-11.patch | 12.95 KB | quicksketch |
#9 | webform-token-replacement-2064851.patch | 11.42 KB | quicksketch |
#7 | move-token-replace-to-render-2064851-6.patch | 1.77 KB | thechanceg |
#4 | move-select-token-replace-to-utalize-node-obj-2064851-3.patch | 1.74 KB | thechanceg |
#2 | date-tokens.png | 84.86 KB | knalstaaf |
Comments
Comment #1
quicksketchI 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:
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.
Comment #2
knalstaaf CreditAttribution: knalstaaf commentedI'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).
Comment #3
quicksketchHi @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.
Comment #4
thechanceg CreditAttribution: thechanceg commentedHere 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().
Comment #5
thechanceg CreditAttribution: thechanceg commentedComment #6
quicksketchThanks @thechanceg! This looks great, exactly what I had in mind.
The only problem here is that we should wrap the token replacement here:
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).Comment #7
thechanceg CreditAttribution: thechanceg commentedThanks 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!
Comment #8
jweowu CreditAttribution: jweowu commentedThe patch works for me. The code looks sane, and has quicksketch's prior approval for the approach, so I think RTBC.
Comment #9
quicksketchThis 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.
Comment #10
jweowu CreditAttribution: jweowu commentedThere'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:Each of those functions takes a
$filter
argument, but none of them uses it in any way.Comment #11
quicksketchThanks @jweowu for the feedback! You're right on all counts. I've rerolled to fix these issues. Great catches.
Comment #12
quicksketchFinally pushed this into the 7.x-4.x branch. Thanks everyone here for their work on this one.
Comment #13
knalstaaf CreditAttribution: knalstaaf commentedCool, 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!
Comment #15
sunshinee CreditAttribution: sunshinee commentedLeaving 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
Comment #16
fenstratNeeds porting to 8.x-4.x.
Comment #17
fenstratCommitted and pushed 0838ce1 to 8.x-4.x. Thanks!