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

Comments

firfin’s picture

I 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?

danchadwick’s picture

Category: Bug report » Feature request

Webform 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.

firfin’s picture

How 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?

danchadwick’s picture

Issue summary: View changes

There 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].

firfin’s picture

The 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.

danchadwick’s picture

I 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.

firfin’s picture

Thanks 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.

danchadwick’s picture

@firfin -- I'm open to this. We are already generating tokens, so making the submission if needed would be the extra work.

firfin’s picture

Assigned: Unassigned » firfin

alright, gonna dive into it this week then.
webform_client_form and the token generation will be some good starting points I hope ;-)

firfin’s picture

jasonawant’s picture

Hi 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.

danchadwick’s picture

I 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.

jasonawant’s picture

Hi Dan,

Thanks for the update! Where can we find the patch you ported? Thanks, Jason

I ported quicksketch's patch from the related issue and it works fine

danchadwick’s picture

Patch 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).

jasonawant’s picture

Hi 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.

danchadwick’s picture

Title: Using tokens in markup fields doesn't work (WF4 style) » Add ability to use submission tokens in markup components
Status: Active » Fixed
StatusFileSize
new32.29 KB

Big patch.

  1. _webform_display_COMPONENTTYPE has a new argument, $submission, which required updating the API and every component. Other components that aren't webform core will continue to work fine.
  2. Using quicksketch's patch, markup components which are displayed in the form have their token replacement deferred until #after_build time, at which point the $form_state is available to provide the values from other components. Very tricky, quicksketch.
  3. Markup components which are displayed in the submission (and for token-generation use) now can reference submission tokens.
  4. webform_token() was modified quite a bit:
    1. The if/then that encompasses almost the whole function was changed to an early return to reduce indentation and increase readability.
    2. The set-up of variable that are used in several places was cleaned up on consolidated.
    3. Recursive loops are detected. In this case, markup components are excluded from the rendered submission to prevent a loop. This is true of both [submission:values] and [submission:values:YOURMARKUPCOMPONENT].

Committed to 7.x-4.x.

danchadwick’s picture

Version: 7.x-4.x-dev » 8.x-4.x-dev
Category: Feature request » Task
Status: Fixed » Patch (to be ported)

  • DanChadwick committed 8970544 on 7.x-4.x
    Issue #2482659 by DanChadwick, quicksketch: Added ability to use...
firfin’s picture

Big 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.

fenstrat’s picture

Version: 8.x-4.x-dev » 7.x-4.x-dev
Category: Task » Feature request
Status: Patch (to be ported) » Fixed

Committed and pushed to 8.x-4-x. Nice work!

  • fenstrat committed 7c0a42f on 8.x-4.x
    Issue #2482659 by DanChadwick, quicksketch: Added ability to use...

Status: Fixed » Closed (fixed)

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