Step 1/5 of #3642269: [meta] Migrate Ajax command attachments to the Render API
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.
A custom Ajax command may want to use the Render API's AttachmentsInterface and AttachmentsTrait to keep its handler library on the command itself and let callers add libraries or settings before adding it to a response.
AjaxResponse already uses that same interface. A caller can attach a library directly to the response with addAttachments(). If the command supplies the library through that interface, however, AjaxResponse::addCommand() adds the command and silently ignores its attachments.
Reason is that addCommand() only collects assets from commands that implement the Ajax-specific CommandWithAttachedAssetsInterface. A custom command that already exposes its libraries and settings through the Render API's getAttachments() must therefore provide a second getter, getAttachedAssets(), for the same assets.
Steps to reproduce
- Create a custom Ajax command that implements
CommandInterfaceand the Render API'sAttachmentsInterface, usingAttachmentsTraitfor the attachment methods. - Call
$command->addAttachments(['library' => ['core/drupal.announce']])and add the command to a freshAjaxResponse. $response->getCommands()contains the rendered command, but$response->getAttachments()returns an empty array.
Proposed resolution
Let AjaxResponse::addCommand() collect attachments through AttachmentsInterface::getAttachments() and merge them with the response's existing attachments. Read them after render(), since rendering the command may itself collect libraries and settings from nested render arrays.
For commands that already implement both attachment interfaces, keep calling getAttachedAssets(). The two getters can return different assets. Switching getters merely because a command already implements AttachmentsInterface could therefore change what an existing response receives.
Regression tests need to cover both cases. One to verify attachments collected during render() reach the response alongside preexisting attachments. The other giving the two getters different libraries and verifying addCommand() collects only the library returned by getAttachedAssets().
As soon as this change lands, custom commands can use the standard interface. The next step in #3642269: [meta] Migrate Ajax command attachments to the Render API lets core commands opt into the same collection path while keeping their legacy getters callable.
Remaining tasks
Review.
API changes
AjaxResponse::addCommand() now collects attachments from commands implementing the Render API's AttachmentsInterface. For commands implementing both attachment interfaces, getAttachedAssets() retains precedence.
Data model changes
None.
Release notes snippet
Custom Ajax commands can now supply libraries and JavaScript settings directly through the Render API's AttachmentsInterface.
Issue fork drupal-3642273
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
Comment #3
pancho