Feature request: I would like to be able to perform additional Ajax actions when a flag is triggered. It is currently possible to do that by extending ActionLinkController::generateResponse, but that forces all of my changes into a single file and I would like to put different functionality in different custom modules.
[EDIT: The override described above was made more difficult by changing generateResponse to private in f71774a2.]
While I could just as easily implement this feature in a custom module, I thought I would submit this as a patch first.
This feature request is essentially the same as #2911409: Allow AjaxResponse to be altered before it's sent, credit should go to @jrockowitz. :)
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | 3062604.patch | 4.94 KB | davemaxg |
| #8 | 3062604.patch | 4.94 KB | batkor |
| #2 | interdiff-3062604-1-2.txt | 2.84 KB | akalata |
| #2 | 3062604-2.patch | 4.33 KB | akalata |
Issue fork flag-3062604
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 #2
akalata commentedForgot to add some helper values for the custom response class.
I'm also seeing in latest -dev that ActionLinkController::generateResponse was made private (was protected in -alpha3), which means that the workaround in the initial request is no longer possible.
Comment #3
akalata commentedComment #4
berdir> I'm also seeing in latest -dev that ActionLinkController::generateResponse was made private (was protected in -alpha3), which means that the workaround in the initial request is no longer possible.
I'd like to see that reverted, feel free to do that here. private shouldn't be used in Drupal unless there's a reason for it. Default is protected. There might be other reasons where overriding the controller would be easier than a response subscriber.
It's still a controller and not really covered by BC, you can do it but your code might break. that's fine.
Comment #5
g089h515r806 commentedis here any document how to use this feature?
I want to perform additional js actions after a flag is triggered, I could not found any document about it.
For Drupal 7 flag module, there are a detail document about this.
Comment #6
loze commentedThis patch applies and works for me.
+1
Thanks
Comment #7
loze commentedIn case anyone is interested, with this patch in place I was able to make a submodule that replaces flags default feedback messages with noty.js style messages.
see: https://github.com/lozeone/drupal8_noty and https://ned.im/noty/#/
take a look at the FlagAjaxResponseSubscriber.php file to see how to add your own ajax commands to the event.
Comment #8
batkorCreate event for alter response on action.
Comment #9
batkorComment #10
eworwa commentedHi, I can confirm the patch in #8 applies without issues and it allows to subscribe to a FlagEvents.
My current set up is
Drupal 9.1.10
Flag 8.x-4.0-beta2
After applying the patch I was able to subscribe to a FlagEvents following instructions in here https://www.drupal.org/docs/creating-custom-modules/subscribe-to-and-dis....
For reference, my EventSubscriber class looks like
Comment #11
very_random_man commentedYes, the patch in #8 works great! Thanks, this was just what i needed! :-D
Comment #12
fmb commentedThanks! RTBC+1
Comment #13
davemaxg commentedThe patch at #8 does not work on Drupal 10.1 which has a newer version of symfony. On Symfony 4.3+ the dispatch method has been changed
from
public function dispatch($eventName, Event $event = null);
to
public function dispatch(object $event, ?string $eventName = null): object;
This new patch addresses that issue. It's a one line change.
Comment #14
malcomio commentedComment #15
loze commentedThe patch in #13 works correctly for drupal 10.1+.
I've been using it in production for a while now, with no issues. Thanks!
Comment #16
ivnishNeeds reroll to MR, needs tests how it works
Comment #20
arantxioI've moved the code from the patch in #13 to the branch and created a PR.
It still needs testing though.
Comment #21
malcomio commentedComment #22
ivnishStill needs tests
Comment #26
deaom commentedCherry picked the commit to open a new MR for 5x, test needed.
Comment #27
deaom commentedAdded a Kernel test that test the response can be altered. Ready for review.