Step 2/5 of #3642269: [meta] Migrate Ajax command attachments to the Render API.

Currently postponed on #3642273: Let AjaxResponse::addCommand() collect attachments from commands implementing Render API’s AttachmentsInterface.

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.

#3642273: Let AjaxResponse::addCommand() collect attachments from commands implementing Render API’s AttachmentsInterface lets commands supply attachments through the Render API.

For commands that already implement both attachment interfaces, AjaxResponse::addCommand() keeps calling getAttachedAssets() to preserve existing behavior. Next question is: how can a command tell the response to use getAttachments() while keeping its old getter callable?

That leaves a command that wants to migrate with a practical problem. It needs to keep its old getter callable while telling the response to start using getAttachments(). Implementing AttachmentsInterface alone cannot express that choice.

AnnounceCommand illustrates what a command needs from the new API. It declares core/drupal.announce for its JavaScript handler. A caller may want to add another library or supply its own implementation of that handler. Today, each call to getAttachedAssets() returns a fresh object with the same library. Changing that object has no effect on what the response receives.

Proposed resolution

  1. Introduce CommandWithAttachmentsInterface as a public type for commands that supply attachments. It extends CommandInterface and the Render API's AttachmentsInterface without adding methods.
  2. Have AjaxResponse::addCommand() call getAttachments() when a command implements the new interface. Otherwise, getAttachedAssets() retains precedence for commands implementing both older interfaces.
  3. Introduce CommandWithAttachmentsTrait to build configurable defaults on the Render API's AttachmentsTrait. A command declares its defaults through getDefaultCommandAttachments(). Callers can extend its attachments with addAttachments() or replace its defaults and any earlier additions with setAttachments().
  4. Migrate AnnounceCommand as the first user. With no caller configuration, the response still receives core/drupal.announce. Keep getAttachedAssets() callable. If an existing subclass overrides that getter, preserve the assets it supplies and merge caller additions after them. An explicit setAttachments() replaces them.

API changes

Commands implementing CommandWithAttachmentsInterface opt into getAttachments() even if they retain their legacy getter.

AnnounceCommand now supports addAttachments() and setAttachments().

Data model changes

None.

Release notes snippet

Callers can now add to or replace an AnnounceCommand's default attachments before adding the command to an Ajax response.

Issue fork drupal-3642278

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

pancho created an issue.