Problem/Motivation
I have a webform with a few fields that can be pre-populated. Using webform_share, I can embed the form with a URL like the following:
<script src="//www.example.net/webform/event_registration/share.js?variant=custom_opt_in&eventid=xxx&marketing_personas=General+Info;Event+Specific"></script>
This will then write the iframe element to the HTML document and correctly include the query parameters in the iframe src.
However, when the query parameters are changed in the script element, a cached version of the form is displayed, with the previous query parameters in the iframe src.
Steps to reproduce
- Create a webform with one or more fields set to
'#prepopulate': true
- Do NOT configure the `form_prepopulate` setting to TRUE.
- Embed the script share code on another web page, adding a prepopulate query parameter to the src attribute (e.g.
?eventid=test
) - Load the hosting webpage to view the embedded form.
- Change the query parameter in the script src.
- Reload the hosting webpage and see the old prepopulated value in the embedded form field.
Looking at the source on a test form, I saw the following, and noticed that the eventid query parameter was different in the script and iframe src attributes:
<script src="//d9.test/webform/share_test/share.js?eventid=bar"></script>
<iframe src="http://d9.test/webform/share_test/share/iframe-resizer/4.2.10?eventid=foo" title="Share Test | Drush Site-Install" class="webform-share-iframe" allow="geolocation; microphone; camera" allowtransparency="true" allowfullscreen="true" style="width: 1px; min-width: 100%; overflow: hidden; height: 473px;" id="iFrameResizer0" scrolling="no" frameborder="0"></iframe>
...
Proposed resolution
Add some additional cache contexts to either the share.js response or the webform entity.
Comment | File | Size | Author |
---|---|---|---|
#17 | webform-3321630-15.patch | 930 bytes | jeffam |
Issue fork webform-3321630
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3321630-share.js-caching-prepopulate changes, plain diff MR !246
Comments
Comment #2
jeffamComment #4
jeffamAdding cache contexts for each of the 'pre-populatable' elements works well, so I went with that in the merge request.
Comment #8
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commentedGood Catch!!!
Comment #12
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commentedThe patch broke webform/tests/src/Functional/Handler/WebformHandlerTest.php
Comment #13
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commentedComment #14
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commentedComment #17
jeffamI looked into this a bit, and found that Webform::getElementsPrepopulate() calls initElements(), which must do something to cause the return value of getElementsDecoded() and getElementsOriginalDecoded() to be identical. Therefore, the hooks (e.g. createElement) in postSave() aren't called.
I'm not familiar enough with the code to figure out how to make the call to getElementsPrepopulate() idempotent, so I'm attaching a patch with a more focused fix for my particular problem. I'm using a patch to trigger a test run. I guess merge requests can't be tested?
Comment #18
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commentedComment #19
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commented