core/lib/Drupal/Core/Form/FormBuilder.php | 6 ++++++ core/modules/comment/src/CommentForm.php | 4 ++++ core/modules/contact/src/MessageForm.php | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 6f31650..51ff254 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -770,6 +770,12 @@ public function prepareForm($form_id, &$form, FormStateInterface &$form_state) { ] ], ); + + // By default, make the form uncacheable. Allow forms to opt in to be + // cached by specifying their own max-age at the top level of the form. + if (!isset($form['#cache']['max-age'])) { + $form['form_token']['#cache']['max-age'] = 0; + } } } diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 51a6cce..8b7ea66 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -10,6 +10,7 @@ use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Unicode; +use Drupal\Core\Cache\Cache; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityConstraintViolationListInterface; @@ -69,6 +70,9 @@ public function __construct(EntityManagerInterface $entity_manager, AccountInter * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { + // This form is cacheable! + $form['#cache']['max-age'] = Cache::PERMANENT; + /** @var \Drupal\comment\CommentInterface $comment */ $comment = $this->entity; $entity = $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId()); diff --git a/core/modules/contact/src/MessageForm.php b/core/modules/contact/src/MessageForm.php index ac435ae..a56fdf9 100644 --- a/core/modules/contact/src/MessageForm.php +++ b/core/modules/contact/src/MessageForm.php @@ -7,6 +7,7 @@ namespace Drupal\contact; +use Drupal\Core\Cache\Cache; use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityManagerInterface; @@ -94,6 +95,9 @@ public static function create(ContainerInterface $container) { * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { + // This form is cacheable! + $form['#cache']['max-age'] = Cache::PERMANENT; + $user = $this->currentUser(); $message = $this->entity; $form = parent::form($form, $form_state, $message);