I have a form that is prepopulated with information specific to an Anonymous user's session information.

However, the form appears to get cached for all anonymous users by default.

'Internal Dynamic Page Cache' is enabled, 'Internal Page Cache' is not.

How do I disable caching of this form? It should be rebuilt fresh every time it is displayed.

  • is it a custom form ?? – Shreya Shetty Sep 14 '16 at 6:03
  • 1
    In your hook_form_alter() do this if($form_id == "my_form") { \Drupal::service('page_cache_kill_switch')->trigger();} – nanbatman Sep 10 at 10:02
up vote 9 down vote accepted

To update:

In order to prevent a form from caching at all, you can include the following #cache key in your form:

$form['#cache'] = ['max-age' => 0];
  • Not workin for me on 8.6 – JorgeeFG Oct 18 at 4:02
  • For anonymous users – JorgeeFG Oct 18 at 4:12

In your mymodule.module file, located at the root of your module directory:

/**
 * Workaround to prevent form caching for anonymous users.
 */
function mymodule_form_myregistration_form_alter() {
    \Drupal::service('page_cache_kill_switch')->trigger();
}

/**
 * Workaround to prevent form caching for anonymous users.
 */
function mymodule_form_mylogin_form_alter() {
    \Drupal::service('page_cache_kill_switch')->trigger();
}

This is using Drupal 8's Form Alter

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.