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.

messages

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

  1. Create patch
  2. Review patch
  3. Test patch
  4. Commit

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3223264

Command icon 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:

Comments

mherchel created an issue. See original summary.

andy-blum’s picture

Assigned: Unassigned » andy-blum
andy-blum’s picture

Status: Active » Needs review
StatusFileSize
new4.3 KB

Context:

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 usinglibraries-override to customize Drupal.theme.message(), but that function in our code is relying on the existence of the class Drupal.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:

  1. Apply patch to latest 9.3.x
  2. 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');
  3. Confirm messages display & close properly
  4. Refresh page and add messages via javascript in the console:

    const messenger = new Drupal.Message;
    messenger.add('js message', {type: 'status'});
    messenger.add('js message', {type: 'error'});
  5. Confirm all messages are displayed & close properly
mherchel’s picture

Status: Needs review » Needs work
+++ b/core/themes/olivero/olivero.libraries.yml
@@ -61,10 +61,11 @@ global-styling:
+    - core/drupal.message

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

andy-blum’s picture

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

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.

mherchel’s picture

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.

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

andy-blum’s picture

I 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.js and drupal-messages-override.es6.js

andy-blum’s picture

Status: Needs work » Needs review
StatusFileSize
new15.96 KB

New patch with a different approach - moving Drupal.theme.message to its own javascript file & library to extend core's drupal.message library. (Patch is not incremental with #3, so no interdiff)

Testing Instructions:

  1. Apply patch to latest 9.3.x
  2. 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');
  3. Confirm messages created server-side display & close properly
  4. Confirm Drupal.Message and Drupal.theme.message are *NOT* available via the console
  5. Add core/drupal.message to olivero's list of libraries in the info file & dump cache
  6. Refresh page and add messages via javascript in the console:

    const messenger = new Drupal.Message;
    messenger.add('js message', {type: 'status'});
    messenger.add('js message', {type: 'error'});
  7. Confirm both php- & js-created messages are displayed & close properly
mherchel’s picture

Status: Needs review » Needs work
+++ b/core/themes/olivero/olivero.libraries.yml
@@ -118,6 +118,11 @@ dropbutton:
+    js/message-override.js: {}

It looks like you forgot to include message-override.js in your patch.

Other than that, this looks good (I still have to test it though)

andy-blum’s picture

Assigned: andy-blum » Unassigned
Status: Needs work » Needs review
StatusFileSize
new27.18 KB
new11.22 KB

Not sure why that wasn't included. New patch attached!

mherchel’s picture

Status: Needs review » Needs work

This breaks the pure JS message tests on https://tugboat-aqrmztryfqsezpvnghut1cszck2wwasr.tugboat.qa/message/js

To reproduce:

  1. Download the CD tools module from https://github.com/zolhorvath/cd_tools
  2. Install the message submodule
  3. Visit /message/js (without the patch). Note that the messages display
  4. Apply the patch and clear the cache
  5. Refresh /message/js, and note that the messages are not present.
andy-blum’s picture

Status: Needs work » Needs review
StatusFileSize
new27.22 KB
new411 bytes

Ah - olivero/messages needs to be there for olivero/drupal.message to work correctly. Added to the patch.

mherchel’s picture

The patch works as expected (yay!), but I'm still having trouble understanding why we need to split this up into two separate JS files.

+++ b/core/themes/olivero/js/messages.js
@@ -24,45 +24,10 @@
+  Drupal.behaviors.messages.closeMessage = closeMessage;

This code in message-override.es6.js assumes that the code in messages.es6.js has already executed. Why do we need to split it up?

andy-blum’s picture

Before 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.messages library, replacing the core Drupal.theme.message() function with our own. This function isn't actually ever used by Olivero, however. This function is used by the Drupal.Message class 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 up Drupal.Message from core/drupal.message

That 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:

  • olivero/messages is loaded from status-message.html.twig
  • Drupal.Message class and Drupal.theme.message() are not sent to the client

and if we need JS message capability:

  • core/drupal.message is extended by olivero/drupal.message
  • olivero/drupal.message loads its dependency olivero/messages
  • Drupal.Message.add() calls Drupal.theme.message() from olivero
  • Drupal.theme.message() calls Drupal.behaviors.messages.closeMessage() from olivero/messages so we keep the close button/functionality in one place.
mherchel’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the explanation. RTBC!

lauriii’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs tests

Is it possible to add tests for this?

mherchel’s picture

andy-blum’s picture

Status: Needs review » Needs work

Moving to needs work since we need tests.

andy-blum’s picture

Status: Needs work » Needs review
StatusFileSize
new35.01 KB
new5.87 KB

My first attempt at writing tests!

Patch is a reroll of #12 and adds testing, interdiff only shows added nightwatch testing.

andy-blum’s picture

StatusFileSize
new81.99 KB

New patch fixes formatting of #19.

Status: Needs review » Needs work

The last submitted patch, 20: 3223264_20.patch, failed testing. View results

andy-blum’s picture

Patch #20 needs a lot of work. The original work in #12 was done before all JS selectors went to the data-drupal-selector format.

andy-blum’s picture

Status: Needs work » Needs review
StatusFileSize
new33.91 KB

Reworked #20 to use the refactored JS & twig, let's see if these tests pass 🤞

andy-blum’s picture

StatusFileSize
new33.88 KB

Attached wrong file.

mherchel’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests

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

andy-blum’s picture

Status: Needs work » Needs review
StatusFileSize
new33.93 KB
new6.02 KB

New patch with updated @file statement & a second with only tests

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kristen pol’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative

Thanks for the update. Patch not applying so needs work.

Added testing steps from #3 to the issue summary.

andy-blum’s picture

Issue summary: View changes

Updating testing instructions in issue summary

yogeshmpawar made their first commit to this issue’s fork.

yogeshmpawar’s picture

Status: Needs work » Needs review
kristen pol’s picture

Issue tags: +Needs manual testing

Tagging for testing.

kristen pol’s picture

Issue summary: View changes

Updated the issue summary to use the template.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mherchel’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new142.85 KB

This looks good (and patch still applies). I tested this in 9.4.x and it worked as expected. Thanks @andy-blum!

mherchel’s picture

StatusFileSize
new33.75 KB

Patch for 10.0.x attached. Same patch but with re-transpiled JS

lauriii’s picture

Issue tags: -Needs manual testing

Tested manually with the steps in issue summary.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Few comments on the MR and the D10 patch needs reroll.

andy-blum’s picture

Status: Needs work » Needs review
kristen pol’s picture

I've reviewed the most recent changes in the MR after the reroll:

1. Drupal.behaviors.messages.closeMessage => Drupal.olivero.closeMessage

2. message-override.js => message.theme.js

and these address the feedback in #39.

Tagging for manual testing. I'll try to do that now.

kristen pol’s picture

Confirmed 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?

kristen pol’s picture

@andy-blum Suggested testing on 9.4 and 10 via Slack.

kristen pol’s picture

I 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 undefined after const messenger = new Drupal.Message; in the console matters?

kristen pol’s picture

Issue summary: View changes

Updated the testing instructions to be more clear.

Successfully tested on 9.5 but still need test on 10.0.

kristen pol’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs manual testing

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

  • lauriii committed fd92a54 on 10.0.x
    Issue #3223264 by andy-blum, mherchel, Kristen Pol: Olivero: Messages...

  • lauriii committed fe141d2 on 9.5.x
    Issue #3223264 by andy-blum, mherchel, Kristen Pol: Olivero: Messages...

  • lauriii committed 59a6000 on 9.4.x
    Issue #3223264 by andy-blum, mherchel, Kristen Pol: Olivero: Messages...
lauriii’s picture

Version: 9.5.x-dev » 9.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed fd92a54 and pushed to 10.0.x. Also committed to 9.5.x and cherry-picked to 9.4.x. Thanks!

Status: Fixed » Closed (fixed)

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

dave reid’s picture

Assigning credit from the previous issue.