Problem/Motivation

Drupal doesn't have just one API for attaching libraries and JavaScript settings to an AjaxResponse. Apart from the widely adopted one in the Render API, we're maintaining a second one specific to Ajax commands.

The two APIs meet in AjaxResponse:

  • A caller can attach a command's handler library directly to the response through the Render API's AttachmentsInterface::addAttachments().
  • A command can declare its own handler library by implementing the Ajax command API's CommandWithAttachedAssetsInterface::getAttachedAssets().
  • AjaxResponse::addCommand() collects assets only from commands that implement that Ajax-specific interface. If a custom command supplies its attachments through the Render API's AttachmentsInterface alone, the response adds the command and silently ignores its attachments.

Leaving the incompatibility aside, even taken on its own merits, the command-specific asset API falls short in at least two respects:

  • CommandWithAttachedAssetsInterface only partially covers the general case.
    When a command supplies libraries or settings, getAttachedAssets() returns them in an AttachedAssets object – just enough for the response to collect the command's libraries and settings. Callers configuring the command, however, have no equivalent of the addAttachments() and setAttachments() methods they can use on the response. For example, changing the object returned by MessageCommand::getAttachedAssets() has no effect on what the response receives. Each call returns a fresh object containing only core/drupal.message.
  • The nominally matching CommandWithAttachedAssetsTrait does provide the needed getter. Instead of completing what the interface leaves out, the trait implements a special case where rendered content, such as a form in a dialog, brings its own assets along.

The names suggest a matching pair, which they are not. Supplying assets does not require a command to render content. Of the five core commands that implement the interface directly, only InsertCommand and OpenDialogCommand use the trait. AnnounceCommand, MessageCommand and TabledragWarningCommand each implement the getter just to declare a single library.

On the other hand, the Render API AttachmentsInterface and AttachmentsTrait already cover the general case of declaring and configuring a command's own attachments perfectly well. Commands that render content could still collect its libraries and settings and pass them on to the response alongside their own.

Bringing commands onto the Render API would let core retire a parallel public API while Ajax commands are still in use. The htmx renderer already carries content attachments through BubbleableMetadata and HtmlResponse, both of which implement AttachmentsInterface. Code that configures libraries and settings through that interface could keep doing so as an interaction moves from Ajax commands to htmx.

Background

In early 2015, #2347469: Rendering forms in AjaxResponses does not attach assets automatically introduced the Ajax interface and trait together so a form rendered by an Ajax command could bring its own assets along.

A few months later, #2407195: Move attachment processing to services and per-type response subclasses moved AjaxResponse onto the Render API's new AttachmentsInterface and AttachmentsTrait. Commands continued to supply assets through the original getter.

In 2019, #3020352: Create a new AnnounceCommand to call Drupal.announce on an AjaxResponse put that getter to a different use. AnnounceCommand implemented it to declare the library for its own JavaScript handler.

Proposed resolution

Use the Render API to manage a command's own attachments. Give commands that supply attachments a public type in CommandWithAttachmentsInterface, which extends CommandInterface and AttachmentsInterface without adding methods. A command can also implement the two parent interfaces directly.

Build on the Render API's AttachmentsTrait so commands can declare defaults. Callers can extend a command's own attachments with addAttachments() or replace its defaults and all earlier additions with setAttachments(). Commands that render content collect its libraries and settings separately. Even after setAttachments([]), the response still receives the libraries and settings an embedded form brings along.

MR 1: Let the response collect attachments through the Render API.
After rendering a command, let AjaxResponse::addCommand() collect its attachments through AttachmentsInterface::getAttachments(). Custom commands can then use the standard interface directly.
To preserve existing behavior for commands that already implement both CommandWithAttachedAssetsInterface and the Render API's AttachmentsInterface, keep calling getAttachedAssets() until the command may opt into the new CommandWithAttachmentsInterface.
MR 2: Introduce CommandWithAttachmentsInterface and CommandWithAttachmentsTrait, and migrate the first command.
When a command adopts the new interface, the response calls getAttachments() even if the command retains its legacy getter. The new trait builds on the Render API's AttachmentsTrait to support configurable defaults.
Have AnnounceCommand implement the interface and use the trait for its default library. Callers can then add to or replace the command's attachments. Keep the legacy getter callable and honor existing subclass overrides.
MR 3: Migrate the other two commands declaring a handler library, and deprecate the legacy getter on all three.
Move MessageCommand and TabledragWarningCommand onto the same contract, preserving existing subclass overrides. Then deprecate getAttachedAssets() on these two commands and AnnounceCommand.
MR 4: Migrate commands that render content.
Introduce RenderableContentTrait for InsertCommand and OpenDialogCommand. It collects the content's libraries and settings separately, so callers can replace a command's own attachments while preserving those of an embedded form.
Deprecate getAttachedAssets() on both commands while continuing to honor existing subclass overrides.
MR 5: Deprecate the shared Ajax-specific asset API.
Deprecate CommandWithAttachedAssetsInterface and its getAttachedAssets() method, along with CommandWithAttachedAssetsTrait and its protected $attachedAssets property. Document the migration paths for commands that declare their own libraries and for those that also collect assets from rendered content.

Related bug fixes

Two bug fixes can proceed independently of the migration:

Comments

pancho created an issue.