Score:2

Custom AJAX Response not firing on Flag action

mw flag

I'm trying to run custom javascript functions at the same time as people interact with certain flags on my site, I have followed this tutorial on how to respond to both Flag/Unflag actions, and this site for how to setup the custom ajax responses, except it just doesn't appear to be able to combine the two

The code inside my FlagSubscriber.php file (from the first tutorial link) looks like the below

class FlagController implements EventSubscriberInterface {

    public function onFlag(FlaggingEvent $event) {    
      dpm('Test Text');

      $response = new AjaxResponse();
      $response->addCommand(
        new ExampleCommand()
      );
      return $response;
    } 

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

It runs the dpm text the next time the page is loaded, but it doesn't seem to be running my custom Ajax response at all, even if I try running a standard Ajax Response from here. Are there some places that you can't run these kind of Ajax Response requests?

Score:1
cn flag

The flagging events don't deal with responses, so you won't be able to return one from there. A different approach is to create a route subscriber, and alter the flag.action_link_flag and flag.action_unlink_flag routes, replacing the controller with your own custom one, e.g.

protected function alterRoutes(RouteCollection $collection) {
  if ($route = $collection->get('flag.action_link_flag')) {
    $route->setDefault('_controller', 'Drupal\custom_module\Controller\CustomController::flag');
  }
  if ($route = $collection->get('flag.action_link_flag')) {
    $route->setDefault('_controller', 'Drupal\custom_module\Controller\CustomController::unflag');
  }
}

Your custom controller would extend the original flag controller (Drupal\flag\Controller\ActionLinkController), use its methods to generate the original response, and then just tack your JS command(s) onto the end.

class CustomController extends ActionLinkController {

  public function flag(FlagInterface $flag, $entity_id) {
    $response = parent::flag($flag, $entity_id);
    $response->addCommand(...);
    return $response;
  }

  // Same for unflag()

}

For full coverage, you might also need to do something similar to the flag.action_link_flag_nojs and flag.action_link_unflag_nojs routes. But if your functionality is purely JS and doesn't need to be present for users without it, probably no need to bother.

Andrew Morris avatar
mw flag
Absolutely amazing, I've just setup some new custom AJAX triggers that are firing as soon as I press the button, which is exactly what I was after. Thank you very much :D
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.