Problem/Motivation

We want to merge an AjaxResponse with another AjaxResponse and this currently not possible.
Inside of MediaLibraryWidget the code is returning an AjaxResponse.
Our code is also returning AjaxReponse and we wanted to merge them.

Steps to reproduce

Trigger an AjaxResponse from two separate sources that need to combine responses.

Proposed resolution

Review merge request.
Added a mergeWith function that merges the current ajax response with another ajax response.

Remaining tasks

Review

User interface changes

N/A

API changes

Introduce a new mergeWith method in

core/lib/Drupal/Core/Ajax/AjaxResponse.php

A new mergeWith method has been added to the AjaxResponse class. This allows developers to merge another ajax response with the current one.

Data model changes

N/A

Release notes snippet

Added mergeWith method to AjaxResponse class to allow merging of AjaxResponses.

Issue fork drupal-3343670

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

Mschudders created an issue. See original summary.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.28 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

enaznin made their first commit to this issue’s fork.

enaznin’s picture

Status: Needs work » Needs review
nod_’s picture

Version: 9.5.x-dev » 10.1.x-dev
Status: Needs review » Needs work

There is an addCommand method that does much more than a simple array_merge so there is bound to be issues doing it this way.

MR should be against 10.1.x since that is the current dev version

Naming of the new function is not ideal. It's not about setting commands, I'd be more of a mergeCommands method
At the moment commands can be added the addCommand method, so what the issue raised could be worked around @Mschudders would that be enough?

mschudders’s picture

@nod thanks for the information.

Indeed the naming was crappy.

Created a new branch against 10.1.x with the bot fixes + naming changes to the function.

Let me elaborate why this is usefull:

I have overridden MediaLibraryWidget so that I can attach an extra Ajax command to the callback of updateWidget:

  public static function updateWidget(array $form, FormStateInterface $form_state) {
    $response = parent::updateWidget($form, $form_state);

    $lb = new LayoutBuilderExtras();
    $responseLbExtraLiveUpdates = $lb->blockAjaxSave($form, $form_state);
    $commandsLiveUpdate = $responseLbExtraLiveUpdates->getCommands();
    $response->mergeCommands($commandsLiveUpdate);

    return $response;
  }

Since updateWidget returns an AjaxReponse already it isn't easy to add an additional command to it.
If you retrieve the commands from AjaxReponse it's already an rendered array and you cannot set them again. That's why I have added a mergeCommands function so that you can merge the commands.
I hope this clarifies things.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

tim-diels made their first commit to this issue’s fork.

tim-diels’s picture

Status: Needs work » Needs review

Rebased to be up-to-date.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Can the MR be updated for 11.x as that's the current development branch.

Also could use a simple test assertion to show it's working.

tim-diels’s picture

Still needs test.

tim-diels’s picture

The module Layout Builder Extras - live update requires this patch.

shalini_jha made their first commit to this issue’s fork.

kriboogh’s picture

StatusFileSize
new656 bytes

Added patch of MR for use in composer.
Applies to both 11.x and 10.3.x

shalini_jha’s picture

Assigned: Unassigned » shalini_jha
shalini_jha’s picture

Assigned: shalini_jha » Unassigned
Status: Needs work » Needs review

I have added test coverage for the mergeCommands method, and the pipeline issues have also been resolved.
I am now moving this for review. Please take a look.

smustgrave’s picture

Status: Needs review » Needs work

Have not reviewed but this one has multiple branches and multiple MRs up those should be cleaned up or summary updated.

shalini_jha’s picture

Assigned: Unassigned » shalini_jha

shalini_jha changed the visibility of the branch 3343670-allow-to-merge-ajaxcommands-10-1-x to hidden.

shalini_jha changed the visibility of the branch 3343670-allow-to-merge to hidden.

shalini_jha’s picture

Issue summary: View changes
shalini_jha’s picture

Issue summary: View changes
shalini_jha’s picture

Assigned: shalini_jha » Unassigned
Status: Needs work » Needs review

I have cleaned up the branch. MR 4902 now contains all required updates and is ready for review. Additionally, I have updated the issue summary for clarity.
Moving this MR to Review. Kindly review the changes.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record, +Needs issue summary update

This appears to be an API change but that section is missing from the summary. Recommend not removing sections from an issue template even if you aren't sure it applies.

API change will need a change record

shalini_jha’s picture

Assigned: Unassigned » shalini_jha
shalini_jha’s picture

Issue summary: View changes
shalini_jha’s picture

Assigned: shalini_jha » Unassigned
Status: Needs work » Needs review

Thank you for your review and feedback; I'll incorporate this. I've updated the issue summary and added a draft mode change request for review.
Please take a look and let me know if there’s anything further needed to update.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs change record, -Needs issue summary update

Tweaked the CR with an example, but rest seems good.

nod_’s picture

Shouldn't that be called mergeResponse?

shalini_jha’s picture

I think the method name mergeCommands was chosen to align with the existing structure of the AjaxResponse class, which focuses on handling and manipulating commands, as seen in methods like addCommand and getCommands. However, if there’s a preference to rename it to mergeResponse for better clarity or to better reflect its purpose, I’m open to making that change. Let me know your thoughts!

catch’s picture

Status: Reviewed & tested by the community » Needs work

This is requiring that the command is rendered before it's merged in. I would expect you could pass actual commands here, not already rendered commands, to match addCommand(). And even if there's a reason that can't be done, the documentation doesn't specify what the actual contents of the argument is.

Also it will result in the attachments logic in AddCommand not running, and the new test coverage does not cover that - it's not clear at all what will happen to the attachments.

@nod_'s mergeResponse() suggestion might be able to handle it, if you passed in the full AjaxResponse object, and this method then extracted both the rendered commands and the attached from it.

Either that, or I think we need to look at refactoring things a bit more, so that commands are not immediately rendered when added, and so that ::getCommands() or a new getAjaxCommands() method can return command objects.

jecet4’s picture

Issue tags: -Needs Review Queue Initiative +Needs Review Queue Initiative #WarsawContributionWeekend2025
jecet4’s picture

Issue tags: -Needs Review Queue Initiative #WarsawContributionWeekend2025 +Needs Review Queue Initiative, +WarsawContributionWeekend2025

graber’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs Review Queue Initiative

Looks good to me, marking as RTBC, thanks!

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update, +Needs change record updates

The issue summary and change record could use an update here since they no longer reflect the approach in the MR.

graber changed the visibility of the branch 3343670-allow-to-merge-ajaxcommands-11-x to hidden.

graber changed the visibility of the branch 11.x to hidden.

graber’s picture

Issue summary: View changes
graber’s picture

Status: Needs work » Needs review

Updated, including tests a bit as well to illustrate direct usage of

return $response->mergeWith($other);
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs issue summary update, -Needs change record updates +Needs Review Queue Initiative

Left a few small comments on the MR

If you are another contributor eager to jump in, please allow the previous poster at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

tim-diels’s picture

Assigned: Unassigned » tim-diels
Issue tags: +ddd2025

I'll update the code to fix the comments

tim-diels’s picture

Assigned: tim-diels » Unassigned

Tests are failing :(

shalini_jha’s picture

Hi, I have re-run the previously failed test and it has now passed. It appears to have been a random failure :)

tim-diels’s picture

Status: Needs work » Needs review

Ok then we can set this to needs review. Thanks.

graber’s picture

Status: Needs review » Reviewed & tested by the community

All threads resolved. Just a small note to @smustgrave: @param doc comments are not needed if parameters are well described by their names and type hints and that was the case here.
Setting to RTBC.

smustgrave’s picture

Do you have a docs or policy page that mentions that by chance?

graber’s picture

Just that I remember some time before @var and @param tags were required by our coding standards and it's no longer the case. Probably because of type hints that make them obsolete in a majority of cases.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Coding standards still require @param everywhere, it will change soon when #3376518: Allow omitting @var, @param and @return tags or their descriptions if the variable name and type is enough is adopted although only in specific circumstances.

This looks good to me. Committed/pushed to 11.x, thanks!

  • catch committed 851df445 on 11.x
    Issue #3343670 by tim-diels, shalini_jha, jecet4, mschudders, graber,...

catch’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.