Drupal is a registered trademark of Dries Buytaert

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.

Activity

Total releases
2
First release
Jan 2025
Latest release
1 year ago
Release cadence
0 days
Stability
50% stable

Releases

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