If there are more type drupal messages on a page, the message type specific divs get too more classes e.g.:
drupal_set_message('MASSEGES TEST STATUS');
drupal_set_message('MASSEGES TEST ERROR', 'error');
drupal_set_message('MASSEGES TEST WARNING', 'warning');
outputs:
<div class="alert alert-success alert-dismissible" role="alert">...</div>
<div class="alert alert-success alert-dismissible alert-danger" role="alert">...</div>
<div class="alert alert-success alert-dismissible alert-danger alert-warning" role="alert">...</div>
Changing
<div{{ attributes.addClass(classes) }} role="alert"> line in templates/system/status-messages.html.twig to this:
<div{{ attributes.setAttribute('class', classes) }} role="alert"> solves this problem.
Comments
Comment #2
alexeiseremet commentedI will try to fix that.
Comment #3
alexeiseremet commentedComment #4
alexeiseremet commentedI fix that.
Comment #5
malaynayak commentedHi @alexeiseremet,
The patch is working for me.
Comment #6
markhalliwellThis base theme only adds these classes directly from the template itself, nowhere else. If you're encountering duplicate classes, then it would seem that somewhere, something is adding this class in something like a preprocess function or statically via a custom template.
Comment #7
markhalliwellComment #8
rpataca commentedI'm running into the same issue. Problem isn't duplicate classes - problem is it's taking every drupal_set_message TYPE on the page and adding classes to the markup as it builds out. I think setAttribute fixes this by setting the proper classes each time and not ADDING the classes as it loops through multiple set_messages on the page.
The classes array is inside the for loop causing each type to add to the array.
Comment #9
i.bajrai commentedI can confirm #4 works and is not an issue with an external template.
I am using the theme pretty much straight out of the box using an inherited theme with almost no customisations.
Comment #10
i.bajrai commentedPatch doesn't apply to 3.9.0, re-rolling
Comment #11
markhalliwellSimply overriding the
classattribute means that any classes originally passed (via preprocess) are lost. We could use thecreate_attributes()twig function here to essentially "clone" the attributes array each loop, but that was only added in 8.3.x and I don't want to set a minimum core version because of that.Instead, we'll have to save any original classes and set it to that, then add the current loop's classes on top of that.
Comment #12
markhalliwell