feat: MCD-1386 Route the message AJAX command into the global messages. - #532
feat: MCD-1386 Route the message AJAX command into the global messages.#532drubot wants to merge 4 commits into
Conversation
A webform managed_file upload error arrives inline: core's ManagedFile::uploadAjaxCallback prepends #type => status_messages into the replaced element's #prefix, so the messenger block renders inside the widget — unstyled on the decoupled frontend, which never loads core's messages CSS. Wrap Drupal.AjaxCommands.insert to lift those .messages blocks into the frontend's global messages (useDrupalCe().getMessages()) and strip them from the inline HTML; wrap .message likewise. Field-level inline errors (.form-item--error-message) stay next to their field.
| } | ||
|
|
||
| /** A message forwarded from a Drupal AJAX response to the frontend. */ | ||
| export interface DrupalForwardedMessage { |
There was a problem hiding this comment.
we should define this is a general DrupalMessage type and use it here + elsewhere where it is used. let'S fix that.
| * (`useDrupalCe().getMessages()`). Registered once via {@link setMessageSink}; | ||
| * a no-op until then. | ||
| */ | ||
| let messageSink: ((messages: DrupalForwardedMessage[]) => void) | null = null |
There was a problem hiding this comment.
nope, our module comes with the sink, it should jsut use that. --> the messages of the fetched page obect and getMessage of use DrupalCe
| * Extracts Drupal messenger messages from an AJAX response's HTML, forwards | ||
| * them to the registered sink, and returns the HTML with those blocks removed. | ||
| * | ||
| * Drupal's managed_file upload AJAX callback prepends `#type => status_messages` |
There was a problem hiding this comment.
this is a general Drupal ajax feature, not limited to managed_file
| * Drupal's managed_file upload AJAX callback prepends `#type => status_messages` | ||
| * into the replaced element's `#prefix` (see | ||
| * \Drupal\file\Element\ManagedFile::uploadAjaxCallback), so upload errors arrive | ||
| * as a `.messages` block inside the inserted markup — unstyled on the decoupled |
There was a problem hiding this comment.
make comment more brief and structured
| * | ||
| * No-op until `core/drupal.ajax` has defined `Drupal.AjaxCommands`. | ||
| */ | ||
| function installAjaxMessageForwarding(): void { |
… response HTML.
…nk indirection. Address review on #532: - Define a general DrupalMessage type (types.d.ts) and reuse it for getMessages(), pushMessagesToState and the menu error handler. - Remove the loader-side message sink (setMessageSink/messageSink): the module already owns the sink via getMessages(). The message command handler now lives in useDrupalCe and pushes into that state directly, installed once after the AJAX library loads. - The handler is a general Drupal AJAX feature, not managed_file specific; comments trimmed accordingly.
|
🤖 Claude Code: addressed the review (head 365c09a).
PR description updated. Suite 162/162 green, eslint clean. The kickstart patch ( |
useState is unreliable after an await (Nuxt instance may be lost), so getMessages() moves ahead of the dynamic import; only the AjaxCommands install (window-only) stays after the library loads. Without this the message command handler could silently fail to install.
|
figured there is already a drupal ajax message command which does the job well, I added as https://www.drupal.org/project/drupal/issues/3618230#comment-16740311 for handling file upoads nicely. |
Delivers Drupal messenger messages carried in AJAX responses (e.g. a webform
managed_fileupload error) into the decoupled frontend's global messages, as structured data.This is the frontend half of MCD-1386; it lets MCD-1377 work end-to-end. The backend half is a core patch (
ManagedFile::uploadAjaxCallbackemits typedMessageCommands instead of renderingstatus_messagesinto the widget#prefix) — see #3457067; mossbo ships it via composer until it lands.What this does
Registers a handler for Drupal's
messageAJAX command (core'sMessageCommand). Core's own handler renders throughDrupal.Messageinto a theme's[data-drupal-messages]region — a decoupled page has neither that region nor core's message CSS — so the payload is routed into the module's existing global messages instead:useDrupalCenext togetMessages()and pushes aDrupalMessagestraight into that state — no separate sink/registration layer. It is installed once, overDrupal.AjaxCommands.prototype.message, right after the AJAX library loads.errorandwarningkept;status/unknown →success.clearPreviousresets the message state first, so a retried upload replaces its earlier error instead of stacking duplicates.managed_file. Theinsertcommand is never touched (no HTML parsing anywhere).Type
A single
DrupalMessagetype ({ type: 'error' | 'warning' | 'success', message }) intypes.d.ts, reused bygetMessages(),pushMessagesToState(the fetched page object's messages) and the menu error handler.Verification
installDrupalMessageCommandunit tests cover: routing into the global messages, type mapping,clearPrevious, no-op beforeDrupal.AjaxCommandsexists, and install-once.🤖 Generated with Claude Code
https://claude.ai/code/session_01Tjk2QnTs86cpmMqAT57NGH