I'm using the Flag module with Views for a product comparison.
Each user is presented with a set of Exposed Filters (i.e., product type, price range, features required), that once selected results in a set of products, presented in a View, that meet the selection criteria. Within the View their is a 'compare' field where the user can set Flag module to 'flag' or 'unflag'. Flagged items will be presented in a subsequent View so that the user can compare attributes on a side by side basis.
What I need to do, however, is to 'unflag' all products when products are selected using Exposed Filters, otherwise previous 'flaggings' may appear in the product comparison, even though they don't meet the current selection criteria.
I'm looking at the Flag API (http://drupal.org/node/305086#flagging) trying to figure out how I can grab all content of type 'Product', check the flag status of each node and set any nodes that are currently set to 'flag' to 'unflag' before anything is presented to the user in the View.
I'm presently using hook_views_pre_render to update some field values for each node in the View. Is there a method I can use to reset all flags to 'unflag' somewhere within this process?
function mymodule_views_pre_render(&$view) {
$results = &$view->result;
if ($view->name == 'compare'){
foreach ($results as $key => $result) {
//updating data fields within each node
}
Comments
Subscribing
Subscribing
It looks like this is best done with "Flag" trim in Rules
On the specified event, you can set a "Trim" Action to set the number of Flags to 0 and that will work. The trick is in setting up the right event on which to execute the Action. The Rules Forms module adds some functionality that might work for my specific use case... I'm trying to work through that right now. Otherwise I'll have to write some module code that creates the specific event. But using the Flag trim action seems the right way to go here.
My site needed an "unflag"
After running around in circles, I finally have unflagging working. You have two options right now (since the Flag module maintainer(s) don't want admins to be able to undo other users' flags):
1. Set the flag to global. Note that existing flags (that have uid != 0 in the flag_content table) won't be unflaggable unless you manually set the uid to 0 in the table. Global flags with uid=0 can be unflagged; non-global flags or global flags with uid=(some other user's id) cannot be unflagged by anyone other than that user. Note that when the flag is global, there is no longer a "flagging user" on file for the flag, so if you have a view that shows users who flagged content, there will be no user to show for that flag. (I.e. if you add a field to a view for the flagging user, your flag record probably won't show up.)
2. Leave the flag not-global. Create a Rules action set that takes in a node, fetches all of the users who have flagged that node. Create a loop through those users, and unflag the node on their behalf. Note that this action occurs using the account of the user(s) who flagged the node, not the admin's account, so the user accounts will need to have permission to unflag content. Then you can add a Bulk Operation field to a view, and have the action point to your Unflag action set.
Mass Unflag
I had to create this functionality for a site that constantly requires mass-updates of nodes, and therefore flags would need to be reset also.
So I just created a quick custom module & form:
Hopefully this helps anyone trying to get this functionality. I made a hook_menu item and then linked to it from a views exposed filter form (that I created to flag content). Judging by the amount of people looking for this I'm surprised its not part of Flags, I may create a module that integrates better, this was just for a one off project.
How to print «delete all»
How to print «delete all» link?