Running custom Javascript on Flag/Unflag

Created on 5 January 2022, over 3 years ago
Updated 19 February 2025, about 2 months ago

Motivation

I'm trying to run additional javascript code when specific flags are triggered on my site, eg User flags Node 1, this runs `runNodeFunction(id)`, User flags Paragraph 5, this runs `runParagraphFunction(id)` etc

Steps to reproduce

I've already applied the patch from this page Allow AjaxResponse to be altered RTBC referring to opening up the AjaxResponse, and that patch seems to have gone in fine...

I've followed this tutorial to create a custom function to listen out for the Flag/Unflag actions on my site, which also seems to be working, I can make it spit out debug messages when the flag is triggered

I've also looked through this tutorial on how to edit the AjaxResponse object

The problem seems to be in tying it all together. if I try to add a new AjaxResponse object inside of the onFlag functions outlined in the second link, it just doesn't seem to run anything at all when I click on the flag. Alternatively if I run a function from within the onResponse outlined in the third link, I don't have enough information I can pull from the flag that triggered it, because I need to know the machine name of the flag, the entity it was flagged on, and the status.

Ideally I'm trying to create a custom module to wrap this in, because I will wish to re-use this configuration on multiple sites, and the machine names of the flags may vary from site to site, but the functions will remain the same

Is there a simpler way I'm missing of doing this?

💬 Support request
Status

Closed: outdated

Component

Flag core

Created by

🇬🇧United Kingdom aps-andrew

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024