Forms can be cached by higher level parts of the render array. We are working on enabling that for a couple forms in #2578855: Form tokens are now rendered lazily, allow forms to opt in to be cacheable. Future versions of 8.x will likely have more form caching, as will custom sites.

When altering forms, cache contexts/tags/max-age must be returned in cases of personalization. This patch adds documentation accordingly.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

moshe weitzman created an issue. See original summary.

moshe weitzman’s picture

Wim Leers’s picture

Priority: Normal » Major
Status: Needs review » Reviewed & tested by the community
Issue tags: -rc target triage +rc eligible, +D8 cacheability, +DX (Developer Experience)

Looks great. RC eligible because docs only patch.

+++ b/core/lib/Drupal/Core/Form/form.api.php
@@ -209,6 +212,8 @@ function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_stat
  * Provide a form-specific alteration instead of the global hook_form_alter().
+ * Implementations are responsible for adding cache contexts/tags/max-age as

Could use a newline in between. Can be fixed on commit.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 2: mention_cache-2614310-2.patch, failed testing.

Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community

Docs-only patch; this cannot actually fail unless bot is drunk.

xjm’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.1.x and 8.0.x, thanks!

Added the missing newline on commit:

diff --git a/core/lib/Drupal/Core/Form/form.api.php b/core/lib/Drupal/Core/Form/form.api.php
index 8f64b23..b950ccb 100644
--- a/core/lib/Drupal/Core/Form/form.api.php
+++ b/core/lib/Drupal/Core/Form/form.api.php
@@ -212,6 +212,7 @@ function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_stat
 
 /**
  * Provide a form-specific alteration instead of the global hook_form_alter().
+ *
  * Implementations are responsible for adding cache contexts/tags/max-age as
  * needed. See https://www.drupal.org/developing/api/8/cache.
  *

  • xjm committed e253d23 on 8.1.x
    Issue #2614310 by moshe weitzman: Mention cache responsibilities of form...

  • xjm committed 7387429 on
    Issue #2614310 by moshe weitzman: Mention cache responsibilities of form...

Status: Fixed » Closed (fixed)

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