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. :)

Issue fork flag-3062604

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

akalata created an issue. See original summary.

akalata’s picture

StatusFileSize
new4.33 KB
new2.84 KB

Forgot 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.

akalata’s picture

Issue summary: View changes
berdir’s picture

Priority: Normal » Major
Status: Needs review » Needs work

> 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.

g089h515r806’s picture

is 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.

loze’s picture

This patch applies and works for me.
+1

Thanks

loze’s picture

In 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.

batkor’s picture

StatusFileSize
new4.94 KB

Create event for alter response on action.

batkor’s picture

Status: Needs work » Needs review
eworwa’s picture

Hi, 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


namespace Drupal\my_module\EventSubscriber;


use Drupal\flag\Event\FlagEvents;
use Drupal\flag\Event\FlagResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Alter a Flag Ajax Response.
 */
class FlagAjaxResponseSubscriber implements EventSubscriberInterface{

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      FlagEvents::RESPONSE => 'onResponse',
    ];
  }

  /**
   * Allows us to alter the Ajax response from a flag.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event process.
   */
  public function onResponse(FlagResponseEvent $event) {
    $response = $event->getResponse();
    $action_type = $event->getActionType(); // either 'flag' or 'unflag'

    if ($response instanceof \Drupal\Core\Ajax\AjaxResponse) {
      if ($action_type == 'flag') {
        // Do something.
      }
      else {
        // Do something else.
      }
    }
  }

}
very_random_man’s picture

Status: Needs review » Reviewed & tested by the community

Yes, the patch in #8 works great! Thanks, this was just what i needed! :-D

fmb’s picture

Thanks! RTBC+1

davemaxg’s picture

StatusFileSize
new4.94 KB

The 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.

malcomio’s picture

loze’s picture

The patch in #13 works correctly for drupal 10.1+.
I've been using it in production for a while now, with no issues. Thanks!

ivnish’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll, +Needs tests

Needs reroll to MR, needs tests how it works

prem suthar made their first commit to this issue’s fork.

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

arantxio’s picture

I've moved the code from the patch in #13 to the branch and created a PR.

It still needs testing though.

malcomio’s picture

Status: Needs work » Needs review
ivnish’s picture

Status: Needs review » Needs work
Issue tags: -Needs reroll

Still needs tests

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

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

deaom’s picture

Version: 8.x-4.x-dev » 5.x-dev
Priority: Major » Normal

Cherry picked the commit to open a new MR for 5x, test needed.

deaom’s picture

Status: Needs work » Needs review

Added a Kernel test that test the response can be altered. Ready for review.