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

fox mulder created an issue. See original summary.

alexeiseremet’s picture

Issue tags: +Moldcamp2017

I will try to fix that.

alexeiseremet’s picture

Status: Active » Needs work
alexeiseremet’s picture

Status: Needs work » Needs review
StatusFileSize
new632 bytes

I fix that.

malaynayak’s picture

Status: Needs review » Reviewed & tested by the community

Hi @alexeiseremet,

The patch is working for me.

markhalliwell’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)

This 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.

markhalliwell’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)
rpataca’s picture

I'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.

i.bajrai’s picture

Status: Closed (works as designed) » Reviewed & tested by the community
StatusFileSize
new64.45 KB
new45.33 KB

I 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.

i.bajrai’s picture

StatusFileSize
new633 bytes

Patch doesn't apply to 3.9.0, re-rolling

markhalliwell’s picture

Assigned: Unassigned » markhalliwell
Status: Reviewed & tested by the community » Needs work
+++ b/templates/system/status-messages.html.twig
@@ -49,7 +49,7 @@
+<div{{ attributes.setAttribute('class', classes) }} role="alert">

Simply overriding the class attribute means that any classes originally passed (via preprocess) are lost. We could use the create_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.

markhalliwell’s picture

Assigned: markhalliwell » Unassigned
Status: Needs work » Fixed
StatusFileSize
new1.66 KB

  • markcarver committed ff8708b on 8.x-3.x
    Issue #2892936 by markcarver, i.bajrai, alexeiseremet: too many classes...

  • markcarver committed c69897b on 8.x-3.x
    Fix indentation
    
    Issue #2892936 by markcarver, i.bajrai, alexeiseremet:...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.