Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Release: Timelinr 1.0.1 Minor update available for module timelinr (1.0.1). Usage Milestone: Simplify Module simplify crossed 10,000 active installs. Usage Milestone: Views Reference Filter Module entityreference_filter crossed 10,000 active installs. Usage Milestone: Dropdown Language Module dropdown_language crossed 10,000 active installs. Usage Milestone: Paragraphs Browser Module paragraphs_browser crossed 10,000 active installs. Usage Milestone: OpenAPI Module openapi crossed 10,000 active installs. Usage Milestone: Decoupled Router Module decoupled_router crossed 10,000 active installs. Usage Milestone: Time Field for Drupal 8+ Module time_field crossed 10,000 active installs. Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

Webform Prepopulate JS

19 sites No security coverage Drupal 9–10 · not 11 Webform ecosystem
View on drupal.org

This module prepopulates Webform fields using JavaScript, directly in the browser. This method avoids issues with cached content and ensures fields are always populated correctly based on URL query parameters. It can be added as a handler to individual Webforms or applied to any form.

The Webform Prepopulate JS module allows you to prepopulate Webform fields on the client side using JavaScript. This approach bypasses Drupal's backend caching mechanisms, ensuring that prepopulated fields work as expected even when using page_cache or dynamic_page_cache.

Features

  • Uses JavaScript to dynamically set Webform field values based on query parameters.
  • Avoids issues with cached values caused by backend prepopulation.
  • Configurable through a Webform handler for better flexibility.
  • Works seamlessly with Drupal's page_cache and dynamic_page_cache layers.
  • Can be applied to any form using hook_form_alter.

Why Use This Module?

While Webform includes a setting to allow elements to be populated using query string parameters (/admin/structure/webform/manage/<form>/settings/form under "Form Behaviors"), there are limitations to this approach:

  1. Even though query string parameters can populate fields, this process happens in the backend, and the query string values are added to the cache context.
  2. For each variation of a query parameter (e.g., utm_source=instagram or utm_source=facebook), a new cache entry is created. This can lead to unnecessary cache fragmentation and performance issues.
  3. So you install the page_cache_query_ignore module to avoid creating new cache entries for specific parameters, those parameters are ignored by the cache system and won’t exist in the backend, meaning the fields will not be populated.

By using Webform Prepopulate JS, the fields are populated directly on the client side, completely bypassing the backend and cache issues. This makes it a more robust solution for scenarios where query parameters are dynamic or varied.

Usage

Webform Integration

  1. Navigate to your Webform settings.
  2. Add the Prepopulate JS Handler to your Webform.
  3. Configure the fields you want to prepopulate using the standard Prepopulate field configuration option.
  4. Pass the values as query parameters in the URL. For example:
    https://example.com/webform-page?field_name=value
  5. The specified fields will be populated dynamically on the client side using JavaScript.

Adding to Any Form

To enable this functionality on other forms, use hook_form_alter in a custom module. Here is an example:

function mymodule_form_alter(&$form, Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id === 'my_custom_form') {
    $form['elements']['my_field]['#attributes']['data-prepopulate-js'] = 'true';
    $form['#attached']['library'][] ='webform_prepopulate_js/webform_prepopulate_js'; 
  } 
} 

This will ensure that the specified field in your custom form is prepopulated using the JavaScript functionality provided by this module.

Troubleshooting

  • Ensure that the Webform Prepopulate JS Handler is enabled for the desired Webform.
  • Verify that the query parameters match the field machine names in the Webform.
  • Ensure the necessary attributes and libraries are added for custom forms.
  • Clear browser and Drupal caches if field values are not being populated as expected.

Depends on

Dependencies of the latest stable release

  • webform Drupal core

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
2
Tracked since
Jan 2025
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 2
Maintenance
Dormant

Releases

Version Type Core Release date
1.0.0 Stable 9–10 Jan 24, 2025
1.0.x-dev Dev 9–10 Jan 24, 2025