Is there any simple way to set / unset flags using JavaScript or jQuery?. I thought this would be easy, by doing something like

var action = Drupal.settings.basePath + 'flag/flag/myFlagType/' + nid;
$.ajax({url:action});

But this gives me a message "Undefined index: token in flag_page()", together with "Access denied". So obviously I have to provide a (session?) token, but I've no idea how to achieve this.

Explanation:
I don't want to use the builtin JS flag links, no links at all, but implement some kind of drag&drop interface using jQuery UI. So I have to implement something like "Flag element, when dropped".

Looked at the Javascript API Docs, but there's no hint about flagging / unflagging, only on how to react on flagging.

Any way, or undocumented function?

Thanks a lot!

Comments

qasimzee’s picture

token = '<?php print flag_get_token($content_id); //Assuming $content_id is already available ?>';
var action = Drupal.settings.basePath + 'flag/unflag/[FLAGNAME]/' + nid + '?token=' + token;

sashken2’s picture

Issue summary: View changes

How to flag / unflag using only JavaScript (without jQuery) in Drupal 8 ?