0

1- I tried to set an error message in Drupal 8 from javascript with the new Messages API using the following code:

const messages = new Drupal.Message();
messages.add('test message');

I get the following error on the console log:

drupal.js?v=8.8.5:13 Uncaught TypeError: Drupal.Message is not a constructor.

How can I include this API?

2- As I can see in message.es6.js, there is an option to set message type: error, warning and status. How to pass these params once I get the function working?

Reference Documentation: https://www.drupal.org/node/2930536

Thank you.

  • What does your .libraries.yml look like? – anonymous Apr 29 at 8:57
0

You need to make sure your library declares a dependency on core/drupal.message, e.g.

foo:
  js:
    js/foo.js: {}
  dependencies:
    - core/drupal.message

Once you've done that, you can choose the message type by including options:

const messages = new Drupal.Message();
messages.add('test message', { type: 'status' });
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.