Account created on 16 June 2023, over 1 year ago
#

Recent comments

Here is a workaround using Javascript.
You only need to include this file on webpages where you want to prevent this bug to happen.
The idea is simple : if the page was loaded from browser cache, i want to force the refresh so drupal may rebuild the form.

/**
 * @file
 * This script forces the page to fully load, without caching.
 * This is helpfull to prevent forms being broken when accessed through the back button.
 */
(function (Drupal) {
  Drupal.behaviors.preventCache = {
    attach: function (context, settings) {
      // Ensure this runs only once per page load
      if (!context.querySelector || context.querySelector('[data-drupal-cache-prevented]')) {
        return;
      }

      // Reload the page if loaded from cache (back/forward navigation)
      window.onpageshow = function (event) {
        if (event.persisted) {
          location.reload();
        }
      };

      // Mark the page to avoid duplicate execution
      document.body.setAttribute('data-drupal-cache-prevented', 'true');
    }
  };
})(Drupal);

After some debug, i finally found hooks that can help modifying these two fields :

  • HOOK_preprocess_input__password
  • HOOK_preprocess_form_element

In my case i wanted to display the label as a placeholder instead. In my .theme file, i added these lines :

function invivogen_preprocess_form_element(&$variables)
{
  if (
    isset($variables['element']['#type']) && (
      $variables['element']['#type'] == 'password_confirm'
      || $variables['element']['#type'] == 'password'
    )
  ) {
    $variables['label_display'] = 'none';
  }


}

function invivogen_preprocess_input__password(&$variables)
{
  $variables['attributes']['placeholder'] = $variables['element']['#title'];
}
Production build 0.71.5 2024