Question

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!

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top