It is possible to use webform submission token values in elements on single page webforms? Looking at a the quick example below, I am attempting to combine the values from elements a
and b
in a third hidden element, c
. When the webform is submitted, the final value for c
is empty. Implementing a solution in code is pretty straightforward, but I am curious if this can be achieved in pure a YAML/UI solution.
a:
'#type': textfield
'#title': 'Textfield A'
'#required': true
b:
'#type': textfield
'#title': 'Textfield B'
'#required': true
c:
'#type': hidden
'#value': '[webform_submission:values:a][webform_submission:values:b]'
When using a multipage webform, the token values are replaced as expected for emelnts on both the first and second pages -- both c
and d
are populated as expected with the combined values of a
and b
.
first_page:
'#type': wizard_page
'#title': 'Page one'
a:
'#type': textfield
'#title': 'Textfield A'
'#required': true
b:
'#type': textfield
'#title': 'Textfield B'
'#required': true
c:
'#type': hidden
'#value': '[webform_submission:values:a][webform_submission:values:b]'
second_page:
'#type': wizard_page
'#title': 'Page two'
d:
'#type': hidden
'#value': '[webform_submission:values:a][webform_submission:values:b]'
Comment | File | Size | Author |
---|---|---|---|
#17 | token_replacement_on-2861976-17.patch | 511 bytes | jrockowitz |
| |||
#16 | Screenshot 2017-03-26 11.36.17.png | 23.28 KB | gg4 |
#14 | webform.webform.abc_.yml | 2.55 KB | jrockowitz |
#10 | token_replacement_on-2861976-10.patch | 647 bytes | jrockowitz |
| |||
#3 | token_replacement_on-2861976-3.patch | 477 bytes | jrockowitz |
|
Comments
Comment #2
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedHmmm... maybe #2758695: Clear tokens with no values was a mistake.
Comment #3
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe attached patch removes the token 'clear' option. Let's see how many test break.
Comment #4
gg4 CreditAttribution: gg4 commentedLooks like you are seeing this as a bug. Moving to needs review to kick the test bot.
Comment #5
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedNot a bug but a bad decision. I think clearing tokens is bad idea and is hindering implementing computed fields, like the one that you included in your example.
Comment #7
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI committed the patch.
Comment #8
gg4 CreditAttribution: gg4 commentedThanks for the quick response and resolution!
Comment #9
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThere is a minor regression that I will fix where the [webform-authenticated-user] are visible on the contact form.
Comment #10
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #12
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #13
gg4 CreditAttribution: gg4 commented@jrockowitz I am still not seeing tokens populate correctly per the original issues report when using the latest dev.
Comment #14
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThis is going to be a lot harder than it seems. We are probably going to need to create a 'computed_value' element.
The immediate problem that I am running into, is that the WebformElement's 'format' methods are not aware of the Webform's submission.
For example the
WebformElement::buildText(array $element, $value, array $options = [])
does not get passed the WebformSubmission entity which would contain the submission values.We also need to decide if using tokens or inline Twig templates (like Views) for the computed value.
Attached is the original example converted to a (broken) test form.
Comment #15
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedBTW, there might be some temporary patch that could get the token replaced on a single page form when a form is validated. I just could not get it to work.
The long term benefit of a computed element is that the result could be dynamic or saved to the database.
Comment #16
gg4 CreditAttribution: gg4 commentedYea this is tricky. I started to look into this as well and will keep digging a bit.
We might want to make sure we are clear on the exceptions around how to deal with empty tokens (#2758695). Seems like ideal state is that they are cleared if empty on form render/display and re-evaluated post-submission for potential replacement according to whether they are
#default_value
or#value
. I don't this current result is desirable. On the current dev, the literal token strings are rendered in visible fields.Comment #17
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe primary use case for tokens is to allow information from the environment, current user, and source entity to passed into a form element or email.
The specific use case that you need is definitely a computed element/value. I think your immediate solution/work-around would be to user a form alter to populate element C.
I also think we need to revert the patch that removed the clearing of tokens.
Comment #18
gg4 CreditAttribution: gg4 commentedAgreed on the revert, and the
form_alter
will likely be fine for my actual use case. I opened this issues originally as a support request because I was unclear if the scenario I outlined was expected behavior or not. If we consider the current functionality expected, I am wondering if it would be helpful to document which tokens users can expect to populate successfully on a single page form and other caveats around their use in#value
and#default_value
properties. Thanks again for your time digging into this issue.Comment #19
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI am rerunning the tests. I think the current functionality make sense but may not be expected. That is why a computed element would solve a lot of confusion because the behavior would be that you enter the tokens or twig variables and the result is always displayed when the element is rendered.
Big picture, tokens need better documentation, especially when it comes to source entity tokens which allow webform nodes to pass field values to embedded webforms.
Comment #21
gg4 CreditAttribution: gg4 commentedDo we want a new issue for the new computed element or should we use this one?
Comment #22
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedYes, the computed element needs a dedicated ticket.
Comment #23
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedNow that computed elements are supported I am going to close this ticket.