messages.es6.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * @file
  3. * Message template overrides.
  4. */
  5. (Drupal => {
  6. /**
  7. * Overrides message theme function.
  8. *
  9. * @param {object} message
  10. * The message object.
  11. * @param {string} message.text
  12. * The message text.
  13. * @param {object} options
  14. * The message context.
  15. * @param {string} options.type
  16. * The message type.
  17. * @param {string} options.id
  18. * ID of the message, for reference.
  19. *
  20. * @return {HTMLElement}
  21. * A DOM Node.
  22. */
  23. Drupal.theme.message = ({ text }, { type, id }) => {
  24. const messagesTypes = Drupal.Message.getMessageTypeLabels();
  25. const messageWrapper = document.createElement('div');
  26. messageWrapper.setAttribute('class', `messages messages--${type}`);
  27. messageWrapper.setAttribute(
  28. 'role',
  29. type === 'error' || type === 'warning' ? 'alert' : 'status',
  30. );
  31. messageWrapper.setAttribute('aria-labelledby', `${id}-title`);
  32. messageWrapper.setAttribute('data-drupal-message-id', id);
  33. messageWrapper.setAttribute('data-drupal-message-type', type);
  34. messageWrapper.innerHTML = `
  35. <div class="messages__header">
  36. <h2 id="${id}-title" class="messages__title">
  37. ${messagesTypes[type]}
  38. </h2>
  39. </div>
  40. <div class="messages__content">
  41. ${text}
  42. </div>
  43. `;
  44. return messageWrapper;
  45. };
  46. })(Drupal);