Form cache from previous pages is not cleared

Created on 19 March 2021, over 3 years ago
Updated 30 January 2023, almost 2 years ago

Problem/Motivation

Each step after the first creates an entry in the key_value_expire table.
That's because simple_multistep_register_next_step() calls setRebuild(), which generates a different build ID for the form.

Drupal clears the cache when the form is submitted, but because simple_multistep rebuilds the cache in a validate function, the submit is never reached until the end of the form. Then the submit only clears the cache of the last page.

This is usually not a problem because the form cache is small and it expires after 6 hours.
However, if you have a big form with a lot of pages, it can use a lot of database space.

Steps to reproduce

  1. Create a form with multiple steps.
  2. Click next on each step.
  3. See that multiple form and form_state entries were created in the key_value_expire table.

Proposed resolution

As a workaround we manually delete the cache entry for the previous page on each form rebuild:

  if ($oldBuildId = $form_state->get('old_build_id')) {
    /** @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface $formCache */
    $formCache = Drupal::service('keyvalue.expirable');

    $formCache->get('form')->delete($oldBuildId);
    $formCache->get('form_state')->delete($oldBuildId);
  }

  $form_state->set('old_build_id', $form['#build_id']);

However, this breaks if the user reloads a page and resends the same POST request (the build ID corresponding to the request does not exist anymore so the first page is displayed instead.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇫🇷France prudloff Lille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024