0

I have been trying to use different status message templates. But I do not know if we can use different status message templates for different pages. Is it possible?

Right now, I am using

status-messages.html.twig

Any help would highly be appreciated. Thanks!

1 Answer 1

3

Here is an example adding routename to status messages' templates suggestions (you may also add some parameters or whatever you need)

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function HOOK_theme_suggestions_status_messages_alter(array &$suggestions, array $variables) {
  $route = \Drupal::routeMatch();
  $suggestions[] = 'status_messages__'.str_replace('.','_', $route->getRouteName());
}

This way you can have status-messages--[routename].html.twig as a template, and if this file not exists, have a fallback to status-messages.html.twig

4
  • umm doesn't all nodes have the same route name...
    – No Sssweat
    Feb 7, 2019 at 10:28
  • 2
    That's probably why i talked about parameters -that $route contains- : i gave an example about how to do it, i'm pretty sure that an @InspiredCoder can add more suggestions and fit his needs this way :-) Feb 7, 2019 at 10:31
  • Thanks a lot! I was wondering how template suggestions would work for status message. You're the man Oulalahakabu :) Feb 7, 2019 at 10:48
  • Note it works always the same, just replace HOOK in hook_theme_suggestions_HOOK_alter by the hook you see in debug mode. You may also use hook_theme_suggestions_alter(array &$suggestions, array $variables, $hook){} Feb 7, 2019 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.