9

I have a form that is pre-populated 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.

1

5 Answers 5

16

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];
5
5

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

1
  • I have a similar problem (using search api) for anonymous users, and this solved it simply. Thank you!
    – hkong
    Mar 7, 2019 at 21:12
5

The correct way to do this is with the following line.

$form['#cache']['contexts'][] = 'session';

See also Cache contexts.

At some stage, max-age has stopped working.

3
  • Yay! This works! I originally commented that it wasn't working, but turned out I did something silly; it does work, so thank you very much.
    – Spudley
    Jul 11, 2019 at 13:21
  • can someone reference where the documentation for this answer is? Feb 17 at 16:28
  • On drupal.org/docs/drupal-apis/cache-api/cache-max-age it states: "Unfortunately, max-age does not work for anonymous users", but I can't remember where I found the session cache context Feb 17 at 16:39
1

Some forms generated by Drupal 8(.7.8)'s FAPI crash when cached server side using Drupal 8's cache, and loaded by an anonymous user.

If you clear the server side cache($ drush cr), the anonymous user can then load the form successfully; on refresh it crashes once cached server side and reloaded by the user.

In this case, BrianV's answer ($form['#cache'] = ['max-age' => 0];) solves the issue on Drupal 8.7.8.

I think we're seeing different types of caching issues here, with different solutions that resolve them, and it might be useful to provide a concise accurate description of your situation which caused a form caching issue, an error log(if it produces errors) as well as the solution you used to fix it.

There might be different problems for a straight FAPI form vs. a webform, vs. say a CiviCRM extension form and providing details will help people a lot more than just saying "well this code worked for my situation" without describing the situation in detail.

Providing the exact Drupal minor version you're on is also important.

The stack trace that my situation was producing was (formatted for readability):

TypeError: Argument 1 passed to Drupal\\Core\\Routing\\RequestContext::fromRequest() must be an instance of Symfony\\Component\\HttpFoundation\\Request, null given, called in DRUPAL_ROOT/core/lib/Drupal/Core/Routing/RequestContext.php on line 31 in DRUPAL_ROOT/core/lib/Drupal/Core/Routing/RequestContext.php on line 37 
#0 DRUPAL_ROOT/core/lib/Drupal/Core/Routing/RequestContext.php(31): Drupal\\Core\\Routing\\RequestContext->fromRequest(NULL)
#1 [internal function]: Drupal\\Core\\Routing\\RequestContext->fromRequestStack(Object(Symfony\\Component\\HttpFoundation\\RequestStack))
#2 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(329): call_user_func_array(Array, Array)
#3 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\\Component\\DependencyInjection\\Container->createService(Array, 'router.request_...')
#4 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(487): Drupal\\Component\\DependencyInjection\\Container->get('router.request_...', 3)
#5 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(326): Drupal\\Component\\DependencyInjection\\Container->resolveServicesAndParameters(Array)
#6 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(502): Drupal\\Component\\DependencyInjection\\Container->createService(Array, 'private__zpo0hT...')
#7 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(237): Drupal\\Component\\DependencyInjection\\Container->resolveServicesAndParameters(Array)
#8 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\\Component\\DependencyInjection\\Container->createService(Array, 'url_generator')
#9 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(487): Drupal\\Component\\DependencyInjection\\Container->get('url_generator', 1)
#10 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(237): Drupal\\Component\\DependencyInjection\\Container->resolveServicesAndParameters(Array)
#11 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\\Component\\DependencyInjection\\Container->createService(Array, 'link_generator')
#12 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(487): Drupal\\Component\\DependencyInjection\\Container->get('link_generator', 1)
#13 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(237): Drupal\\Component\\DependencyInjection\\Container->resolveServicesAndParameters(Array)
#14 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\\Component\\DependencyInjection\\Container->createService(Array, 'plugin.manager....')
#15 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(487): Drupal\\Component\\DependencyInjection\\Container->get('plugin.manager....', 1)
#16 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(237): Drupal\\Component\\DependencyInjection\\Container->resolveServicesAndParameters(Array)
#17 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\\Component\\DependencyInjection\\Container->createService(Array, 'geocoder')
#18 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(487): Drupal\\Component\\DependencyInjection\\Container->get('geocoder', 1)
#19 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(237): Drupal\\Component\\DependencyInjection\\Container->resolveServicesAndParameters(Array)
#20 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\\Component\\DependencyInjection\\Container->createService(Array, 'nfb.address_ada...')
#21 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(487): Drupal\\Component\\DependencyInjection\\Container->get('nfb.address_ada...', 1)
#22 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(237): Drupal\\Component\\DependencyInjection\\Container->resolveServicesAndParameters(Array)
#23 DRUPAL_ROOT/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\\Component\\DependencyInjection\\Container->createService(Array, 'nfb_contact_inf...')
#24 DRUPAL_ROOT/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php(80): Drupal\\Component\\DependencyInjection\\Container->get('nfb_contact_inf...')
#25 [internal function]: Drupal\\Core\\Form\\FormBase->__wakeup()
#26 DRUPAL_ROOT/core/lib/Drupal/Core/Cache/DatabaseBackend.php(167): unserialize('O:31:"Drupal\\\\Co...')
#27 DRUPAL_ROOT/core/lib/Drupal/Core/Cache/DatabaseBackend.php(122): Drupal\\Core\\Cache\\DatabaseBackend->prepareItem(Object(stdClass), false)
#28 DRUPAL_ROOT/core/lib/Drupal/Core/Cache/DatabaseBackend.php(92): Drupal\\Core\\Cache\\DatabaseBackend->getMultiple(Array, false)
#29 DRUPAL_ROOT/core/modules/page_cache/src/StackMiddleware/PageCache.php(306): Drupal\\Core\\Cache\\DatabaseBackend->get('https://dev.nfb...', false)
#30 DRUPAL_ROOT/core/modules/page_cache/src/StackMiddleware/PageCache.php(124): Drupal\\page_cache\\StackMiddleware\\PageCache->get(Object(Symfony\\Component\\HttpFoundation\\Request))
#31 DRUPAL_ROOT/core/modules/page_cache/src/StackMiddleware/PageCache.php(82): Drupal\\page_cache\\StackMiddleware\\PageCache->lookup(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)
#32 DRUPAL_ROOT/core/modules/ban/src/BanMiddleware.php(50): Drupal\\page_cache\\StackMiddleware\\PageCache->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)
#33 DRUPAL_ROOT/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\\ban\\BanMiddleware->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)
#34 DRUPAL_ROOT/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\\Core\\StackMiddleware\\ReverseProxyMiddleware->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)
#35 DRUPAL_ROOT../vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\\Core\\StackMiddleware\\NegotiationMiddleware->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)
#36 DRUPAL_ROOT/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\\StackedHttpKernel->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)
#37 DRUPAL_ROOT/index.php(19): Drupal\\Core\\DrupalKernel->handle(Object(Symfony\\Component\\HttpFoundation\\Request))
#38 {main}
0

In order to prevent a form from caching at all, you can include the following #cache key in your form along with max-age & context attributes:

$form['#cache'] = [
 'max-age' => 0,
  'contexts' => ['session'],
];

Informative Tips: Follow these documentation to know more about context & max-age attributes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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