Problem/Motivation
Split off from #3212975: Olivero: Normalize JavaScript selectors in messages.es6.js
I'm noticing something a bit strange regarding the messages.
Everything works fine when messages are added in from PHP or from JS via Drupal.Message, but JS messages added in when PHP messages are already present are not properly placed. Screenshots are attached. All messages close as expected, and "nested" messages close when their parent is closed.

Steps to reproduce
1. Make sure Olivero is the default frontend theme.
2. Apply patch to latest 9.4.x. Note there are 2 patches (one for D9 and one for D10).
3. Add status/warning/error messages via PHP. In olivero.theme's preprocess_html function:
$messenger = Drupal::messenger();
$messenger->addStatus('PHP Status message');
$messenger->addWarning('PHP Warning message');
$messenger->addError('PHP Error message');
4. Confirm messages display & close properly.
5. Add the core drupal.message library to Olivero's olivero.info.yml libraries
libraries:
- olivero/global-styling
- core/drupal.message
6. Clear the cache.
7. Refresh page and add messages via javascript in the console:
const messenger = new Drupal.Message;
messenger.add('js status message', {type: 'status'});
messenger.add('js error message', {type: 'error'});
8. Confirm all messages are displayed & close properly.
Proposed resolution
Update the code to handle messages coming from both PHP and JavaScript at the same time.
Remaining tasks
Create patchReview patch- Test patch
- Commit
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #45 | Screen Shot 2022-06-03 at 3.47.09 PM.png | 211.72 KB | kristen pol |
| #45 | Screen Shot 2022-06-03 at 3.42.28 PM.png | 131.19 KB | kristen pol |
| #37 | 3223264-10.0.x-37.patch | 33.75 KB | mherchel |
| #36 | Status_Messages___Olivero.png | 142.85 KB | mherchel |
| #26 | 3223264_26--tests-only.patch | 6.02 KB | andy-blum |
Issue fork drupal-3223264
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3223264-olivero-messages-can
changes, plain diff MR !1914
- 3223264-10.0.x
changes, plain diff MR !2337
Comments
Comment #2
andy-blumComment #3
andy-blumContext:
Patch attached to fix the issue. The root of this issue is that the core Drupal.Message.add() was anticipating one more level of wrapping divs than Olivero was providing, so wrapping the iterator with another div fixed the issue (compare to Bartick's status-messages.html.twig).
In addition to the additional markup, I've added core/drupal.message as a dependency of Olivero's global styles library. In our .info file we're using
libraries-overrideto customizeDrupal.theme.message(), but that function in our code is relying on the existence of the classDrupal.Message. As such, instead of calling our own library (olivero/messages), we should call core's library and then allow the override to replace what it needs to.Testing Instructions:
olivero.theme's preprocess_html function:Comment #4
mherchelI don't like the idea of adding the core/drupal.message library to global. I looked at Bartik & other themes, and it's not a default library there.
Is this needed?
Comment #5
andy-blumI agree, it definitely feels heavy-handed. To avoid declaring that as a dependency, we'll probably need to refactor olivero's messages.es6.js file to avoid using the
Drupal.Messageclass.The solution we discussed on our call was to add
{{ attach_library('core/drupal.message') }}in status-messages.html.twig, but this will lead to having the ability to add messages via JS when PHP adds messages, and not if there are no server-side messages.Comment #6
mherchelIt feels like this is a separate issue (that's might be bigger than our theme). Lets fix the PHP + JS display issue here, and open up a followup issue for the ability to set JS messages if there are no PHP messages present.
Comment #7
andy-blumI don't think it's bigger than olivero, I think it's just that olivero is bundling functionality overriding core/drupal.message with custom additional functionality to dismiss system messages.
We might just want to split that file into
messages.es6.jsanddrupal-messages-override.es6.jsComment #8
andy-blumNew patch with a different approach - moving
Drupal.theme.messageto its own javascript file & library to extend core's drupal.message library. (Patch is not incremental with #3, so no interdiff)Testing Instructions:
olivero.theme's preprocess_html function:Drupal.MessageandDrupal.theme.messageare *NOT* available via the consolecore/drupal.messageto olivero's list of libraries in the info file & dump cacheComment #9
mherchelIt looks like you forgot to include
message-override.jsin your patch.Other than that, this looks good (I still have to test it though)
Comment #10
andy-blumNot sure why that wasn't included. New patch attached!
Comment #11
mherchelThis breaks the pure JS message tests on https://tugboat-aqrmztryfqsezpvnghut1cszck2wwasr.tugboat.qa/message/js
To reproduce:
/message/js(without the patch). Note that the messages display/message/js, and note that the messages are not present.Comment #12
andy-blumAh - olivero/messages needs to be there for olivero/drupal.message to work correctly. Added to the patch.
Comment #13
mherchelThe patch works as expected (yay!), but I'm still having trouble understanding why we need to split this up into two separate JS files.
This code in
message-override.es6.jsassumes that the code inmessages.es6.jshas already executed. Why do we need to split it up?Comment #14
andy-blumBefore splitting the js into two files we were doing two different - and mostly unrelated - things.
On the one hand we're creating
closeMessage()and putting it into the "messages" behavior. This function creates a new DOM element (the close button) & event handler (the close button's action) for each message created by PHP.On the other hand, we're extending core's
drupal.messageslibrary, replacing the coreDrupal.theme.message()function with our own. This function isn't actually ever used by Olivero, however. This function is used by theDrupal.Messageclass when a new message is created via javascript, and overrides the core version so new messages match the markup/classes olivero needs to display the messages properly. So we don't need this function unless we're loading upDrupal.Messagefrom core/drupal.messageThat being said, our version of Drupal.theme.message() needs to be able to pull in the closeMessage() functionality from the other javascript file to wire up the click event without duplicating code, hence the dependency declaration.
With this setup, then if we only have PHP messages:
and if we need JS message capability:
Comment #15
mherchelThanks for the explanation. RTBC!
Comment #16
lauriiiIs it possible to add tests for this?
Comment #17
mherchelComment #18
andy-blumMoving to needs work since we need tests.
Comment #19
andy-blumMy first attempt at writing tests!
Patch is a reroll of #12 and adds testing, interdiff only shows added nightwatch testing.
Comment #20
andy-blumNew patch fixes formatting of #19.
Comment #22
andy-blumPatch #20 needs a lot of work. The original work in #12 was done before all JS selectors went to the data-drupal-selector format.
Comment #23
andy-blumReworked #20 to use the refactored JS & twig, let's see if these tests pass 🤞
Comment #24
andy-blumAttached wrong file.
Comment #25
mherchelThis looks fantastic! Great tests, adding an assertion, etc :)
The only minor nitpick is to change the comment on messages.es6.js from "Customization of messages." to something like "Adding a remove button to each message.", so its very clear what the functionality is.
The other minor note is to upload the test changes individually (without the Olivero fix) so that the committers can see the test failure. Upload that in addition to the normal patch.
Thanks so much!
Comment #26
andy-blumNew patch with updated @file statement & a second with only tests
Comment #28
kristen polThanks for the update. Patch not applying so needs work.
Added testing steps from #3 to the issue summary.
Comment #29
andy-blumUpdating testing instructions in issue summary
Comment #32
yogeshmpawarComment #33
kristen polTagging for testing.
Comment #34
kristen polUpdated the issue summary to use the template.
Comment #36
mherchelThis looks good (and patch still applies). I tested this in 9.4.x and it worked as expected. Thanks @andy-blum!
Comment #37
mherchelPatch for 10.0.x attached. Same patch but with re-transpiled JS
Comment #38
lauriiiTested manually with the steps in issue summary.
Comment #39
lauriiiFew comments on the MR and the D10 patch needs reroll.
Comment #41
andy-blumComment #42
kristen polI've reviewed the most recent changes in the MR after the reroll:
1.
Drupal.behaviors.messages.closeMessage=>Drupal.olivero.closeMessage2.
message-override.js=>message.theme.jsand these address the feedback in #39.
Tagging for manual testing. I'll try to do that now.
Comment #43
kristen polConfirmed that D9 patch applies cleanly to 9.4 and 9.5 and D10 patch applies cleanly to 10.0.
Should this be tested on 9.4, 9.5, and/or 10?
Comment #44
kristen pol@andy-blum Suggested testing on 9.4 and 10 via Slack.
Comment #45
kristen polI tested on 9.4 using the testing instructions from the issue summary and all messages render okay and can be closed.
Not sure if the
undefinedafterconst messenger = new Drupal.Message;in the console matters?Comment #46
kristen polUpdated the testing instructions to be more clear.
Successfully tested on 9.5 but still need test on 10.0.
Comment #47
kristen polTested on 10.0.x and it works fine as well.
The only thing I'm not sure of is if we need to show that a test-only patch fails before this fix, but I'm marking RTBC based on the following.
1. Code was reviewed previously.
2. Feedback was addressed from review.
3. Successfully tested on 9.4, 9.5, and 10.
4. Issue summary is clear and includes steps to reproduce.
5. Patch only addresses the issue noted in the issue summary.
6. Tests have been added.
Comment #51
lauriiiCommitted fd92a54 and pushed to 10.0.x. Also committed to 9.5.x and cherry-picked to 9.4.x. Thanks!
Comment #55
dave reidAssigning credit from the previous issue.