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.

Issue fork webform-3321630

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jeffam created an issue. See original summary.

jeffam’s picture

Version: 6.2.x-dev » 6.x-dev

jeffam’s picture

Status: Active » Needs review

Adding cache contexts for each of the 'pre-populatable' elements works well, so I went with that in the merge request.

  • 71ff46e committed on 6.1.x
    Issue #3321630 by jeffam: share.js incorrectly caches webforms with...

  • 71ff46e committed on 6.x
    Issue #3321630 by jeffam: share.js incorrectly caches webforms with...

  • 71ff46e committed on 6.2.x
    Issue #3321630 by jeffam: share.js incorrectly caches webforms with...
jrockowitz’s picture

Status: Needs review » Fixed

Good Catch!!!

  • 7918773 committed on 6.1.x
    Revert "Issue #3321630 by jeffam: share.js incorrectly caches webforms...

  • 7918773 committed on 6.x
    Revert "Issue #3321630 by jeffam: share.js incorrectly caches webforms...

  • 7918773 committed on 6.2.x
    Revert "Issue #3321630 by jeffam: share.js incorrectly caches webforms...
jrockowitz’s picture

Status: Fixed » Needs work

The patch broke webform/tests/src/Functional/Handler/WebformHandlerTest.php

jrockowitz’s picture

Status: Needs work » Needs review
jrockowitz’s picture

Status: Needs review » Needs work

The last submitted patch, 14: webform-3321630-14.patch, failed testing. View results

jeffam’s picture

Status: Needs work » Needs review
FileSize
930 bytes

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

jrockowitz’s picture

Version: 6.x-dev » 6.1.x-dev
Status: Needs review » Reviewed & tested by the community
jrockowitz’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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