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
- Introduce
CommandWithAttachmentsInterfaceas a public type for commands that supply attachments. It extendsCommandInterfaceand the Render API'sAttachmentsInterfacewithout adding methods. - Have
AjaxResponse::addCommand()callgetAttachments()when a command implements the new interface. Otherwise,getAttachedAssets()retains precedence for commands implementing both older interfaces. - Introduce
CommandWithAttachmentsTraitto build configurable defaults on the Render API'sAttachmentsTrait. A command declares its defaults throughgetDefaultCommandAttachments(). Callers can extend its attachments withaddAttachments()or replace its defaults and any earlier additions withsetAttachments(). - Migrate
AnnounceCommandas the first user. With no caller configuration, the response still receivescore/drupal.announce. KeepgetAttachedAssets()callable. If an existing subclass overrides that getter, preserve the assets it supplies and merge caller additions after them. An explicitsetAttachments()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
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