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.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | 3343670-18-MR4902-11.x.diff | 656 bytes | kriboogh |
| #3 | 3343670-nr-bot.txt | 1.28 KB | needs-review-queue-bot |
Issue fork drupal-3343670
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
needs-review-queue-bot commentedThe 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.
Comment #5
enaznin commentedComment #6
nod_There is an
addCommandmethod 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
mergeCommandsmethodAt the moment commands can be added the addCommand method, so what the issue raised could be worked around @Mschudders would that be enough?
Comment #7
mschudders commented@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
MediaLibraryWidgetso that I can attach an extra Ajax command to the callback of updateWidget:Since
updateWidgetreturns 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.
Comment #11
tim-dielsRebased to be up-to-date.
Comment #12
smustgrave commentedCan 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.
Comment #14
tim-dielsStill needs test.
Comment #15
tim-dielsThe module Layout Builder Extras - live update requires this patch.
Comment #17
kriboogh commentedAdded patch of MR for use in composer.
Applies to both 11.x and 10.3.x
Comment #18
shalini_jha commentedComment #19
shalini_jha commentedI 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.
Comment #20
smustgrave commentedHave not reviewed but this one has multiple branches and multiple MRs up those should be cleaned up or summary updated.
Comment #21
shalini_jha commentedComment #24
shalini_jha commentedComment #25
shalini_jha commentedComment #26
shalini_jha commentedI 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.
Comment #27
smustgrave commentedThis 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
Comment #28
shalini_jha commentedComment #29
shalini_jha commentedComment #30
shalini_jha commentedThank 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.
Comment #31
smustgrave commentedTweaked the CR with an example, but rest seems good.
Comment #32
nod_Shouldn't that be called mergeResponse?
Comment #33
shalini_jha commentedI 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!
Comment #34
catchThis 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.
Comment #35
jecet4 commentedComment #36
jecet4 commentedComment #38
graber commentedLooks good to me, marking as RTBC, thanks!
Comment #39
catchThe issue summary and change record could use an update here since they no longer reflect the approach in the MR.
Comment #42
graber commentedComment #43
graber commentedUpdated, including tests a bit as well to illustrate direct usage of
Comment #44
smustgrave commentedLeft 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!
Comment #45
tim-dielsI'll update the code to fix the comments
Comment #46
tim-dielsTests are failing :(
Comment #47
shalini_jha commentedHi, I have re-run the previously failed test and it has now passed. It appears to have been a random failure :)
Comment #48
tim-dielsOk then we can set this to needs review. Thanks.
Comment #49
graber commentedAll 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.
Comment #50
smustgrave commentedDo you have a docs or policy page that mentions that by chance?
Comment #51
graber commentedJust 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.
Comment #52
catchCoding 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!
Comment #55
catch