I thought this question was asked before. But the only answers I can find are either not clear enough or too old (pre-dating webform 4.x) But it doesn't seem possible to u]se tokens in markup fields?
Earlier (at least with WF3, but I believe with early versions of WF4 too) I was able to accomplish this using Webforms_tokens contrib module. That I can't get that to play nice with WF4. So this was possible before, but not anymore.
I found this to be the case on multiple local and server installs and also when testing with simplytest.me.
Use case:
Providing feedback during a multiple page webform.
Expected behaviour:
when using a token in a markup field, the token gets substituted for the value filled out in the component.
Observed behaviour:
Token doesn't get replaced. But neither does it show as [submission:mycomponent] it just gets filtered out. The token does get replaced on confirmation page and in emails.
Steps to reproduce:
- install D7 and WF4.8
- create a new webform, make sure 'save as draft' is checked in the form settings tab.
- add a textfield component 'text1' and a pagebreak component after that.
- finally add a markup component, put in some text and some tokens ( e.g. [submission:values:text1] )
- save and fill out the form
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | webform-markup_submission_tokens-2482659-16.patch | 32.29 KB | danchadwick |
Comments
Comment #1
firfin commentedI am pretty sure this issue was discussed before (for the WF3 release at least.) Just can't seem to find it.
Depending on the answer this might not even classify as a bug. Or at least not in the code but in the documentation. Or it might even be a feature request for documentation. I could add a page to the handbook in that case?
Comment #2
danchadwick commentedWebform does not support the use of submission tokens in component defaults. It never has.
Markup components don't store any data, including defaults. That why they work in emails, and in confirmation pages.
Comment #3
firfin commentedHow do you mean markup doesn't store any data? It has a textarea to store text in. Why couldn't (or shouldn't) this also include tokens? Or do you mean it doesn't store user input data?
Anyway it is a shame this isn't possible. I think it is a really nice to be able to provide feedback to users of the form. Do you agree?
Like I said it used to work with webform_tokens, so I have sites using this functionality already which probably have to be updated to WF4 at some point. So, a feature request indeed. I am willing to pour some time into this. But honestly I don't really have a clue where to start. How would you go about this?
Comment #4
danchadwick commentedThere are numerous difficulties. First, a submission has not necessarily been created yet when the form is created (webform_client_form()). Therefore there isn't yet a submission to give to the page. Also, obviously tokens from the current page can't be used -- only previous page(s).
Markup components, like fieldsets, store no data in the database. They are for display only (and in the case of fieldsets, grouping fields for conditionals). Their value is calculated at display time.
By contrast, a textfield component contains data. The default may be based upon tokens, but not webform submission tokens. (for example, [current-page:user].
Comment #5
firfin commentedThe first problem is not a problem in my use case, providing feedback on later feedback. Of course the Automatically save as draft between pages and when there are validation errors option should be checked in the forms Advanced settings. We could generate the tokens when the draft is saved right?
So when the value of a markup component is calculated at display time the needed values could be taken from that saved draft?
There are probably the difficulties here of course. But you think it is feasible in this way?
Would need to document that 'save drafts' needs to be checked for [draft:tokens] to work on subsequent pages of course.
Comment #6
danchadwick commentedI don't think you want to require drafts, but rather create a faux submission if needed for the purpose of generating tokens. This is already done for preview (see webform_client_form).
Of course, the sid won't be available if there isn't a draft yet.
Comment #7
firfin commentedThanks for clarifying Dan, I will look into webform_client_form.
Is this feature request something you would like to have in webform? I can imagine generating tokens on every 'next page' comes with some performance penalties. So I think quicksketch won't be too amused by that.
This might be better suited for a custom module otherwise. Or do you think this would be interesting enough for a contributed module? webform_presubmission_tokens might be a good name then.
Comment #8
danchadwick commented@firfin -- I'm open to this. We are already generating tokens, so making the submission if needed would be the extra work.
Comment #9
firfin commentedalright, gonna dive into it this week then.
webform_client_form and the token generation will be some good starting points I hope ;-)
Comment #10
firfin commentedComment #11
jasonawantHi firfin,
Any progress on this issue?
I found this older issue #1672890-16: Access submission data tokens from earlier pages in markup components, which has code in it. Jason.
Comment #12
danchadwick commentedI ported quicksketch's patch from the related issue and it works fine, although creating a submission for each rendering of a markup form element definitely needs some caching. He used a very clever trick of creating a post build function which has sufficient context to create the submission for the tokens. Nice.
Unfortunately displaying a markup item (not in a form) does not have any such context. This would mean either a rather substantial API change (which would be backward compatible) or a hideous static variable hack to set the submission and then retrieve it from a global function. I'm not very happy about either.
There is a deep assumption in the component API that components don't need to know about other components or the submission. Creating these tokens breaks that assumption.
Comment #13
jasonawantHi Dan,
Thanks for the update! Where can we find the patch you ported? Thanks, Jason
Comment #14
danchadwick commentedPatch still in progress. There is an issue with markup components that display their value. In order to display the value, you need to replace the tokens. In order to replace the tokens, you need to display each component, including markup items. Infinite recursion loop.
One alternative is to omit markup items from the token values, but this breaks the existing [submission:values] token with does include markup items (if configured for display and not just form).
Comment #15
jasonawantHi Dan,
Thanks again for the update. I'm willing to help out with a review and code when ready.
I guess I'm not that familiar with the webform to know what "if configured for display and not just form" means. I'll take a look. Jason.
Comment #16
danchadwick commentedBig patch.
Committed to 7.x-4.x.
Comment #17
danchadwick commentedComment #19
firfin commentedBig patch, heh I would call it huge. But then there was a lot more work needed than I would have thought.
Thanks again for your effort Dan!
I just tested the latest dev version (4.9+8) and it is working like a charm!
Gonna deploy it on two other drupal sites tomorrow and let you know how it goes.
Comment #20
fenstratCommitted and pushed to 8.x-4-x. Nice work!