I've created a custom module which does some additional form handling and styling required for my project - this is all working fine.
Due to the size of the form - 50 odd fields - I'm setting up a Twig file in my module for the confirmation screen as it is far easier to manage it in my IDE than using the GUI. My confirmation page lists all the values styled in a specific way and laid out in a specific way. I've technically got it working by using hook_theme_suggestions_webform_confirmation_alter
and hook_theme
to point to my Twig template.
However, I've just recently discovered that, due to Drupal 8 internal page caching being so aggressive for anonymous users, the confirmation page is getting cached for everyone regardless of the actual submission values. So the first user gets it fine but the second submission from then on gets the cached version from the first.
I've read in other places to make use of hook_preprocess_webform_confirmation
and set $variables['message']['#cache']['max-age'] = 0;
. However, this is never called which I suspect is due to how I went about using my Twig template mentioned above.
Can someone help point me in the right direction here. Am I supposed to setup some kind of overwriting route in order to change the theme file or is it just a matter of tweaking what I've already done?
Functions in my .module
file
function application_theme($existing, $type, $theme, $path) {
$info = [
'gib_application' => [
'render element' => 'form',
'template' => 'application-form'
],
'application_form_confirmation' => [
'template' => 'application-form-confirmation'
]
];
return $info;
}
And
function application_theme_suggestions_webform_confirmation_alter(array &$suggestions, array $variables) {
if (empty($variables['header'])) {
$suggestions[] = 'application_form_confirmation';
}
}
UPDATE
I've tweaked my hook_theme
function to the below and also disabled hook_theme_suggestions_webform_confirmation_alter
assuming it isn't relevant so long as hook_theme
is defining the correct name.
function application_theme($existing, $type, $theme, $path) {
$info = [
'gib_application' => [
'render element' => 'form',
'template' => 'application-form'
],
'webform_confirmation__application_form' => [
'template' => 'application-form-confirmation',
'base hook' => 'webform_confirmation'
],
];
return $info;
}
Here are the Twig suggestions:
<!-- FILE NAME SUGGESTIONS:
* webform-confirmation--application-form.html.twig
x webform-confirmation.html.twig
-->
Unfortunately now my Twig template isn't being loaded at all now (due to dropping hook_theme_suggestions_webform_confirmation_alter
.
$variables['message']
is wrong. Please retry it with$variables['#cache']['max-age'] = 0;
.webform_confirmation__
and instead of defining your own variables reference the base hook. Then the preprocess hook you are trying to invoke should work. See drupal.stackexchange.com/questions/267263/…template
in theme_hook() is not necessary, but you need the suggestion. The linked example is different because there the suggestion for the content type already exists.template
, and then renaming my Twig filename accordingly, it is now loading the Twig template from my module while also allowing me to usehook_preprocess_webform_confirmation
so that I can disable cache.Unfortunately thepreprocess
hook isn't called everytime for anonymous users which means I can't use it to disable cache! That is a different question though which I'll look into.