I've used the Flag module with Computed Field and Views to create a simple enquiry system, similar to a shopping cart:

  1. User browses the site, flagging nodes they're interested in
  2. User submits an Enquiry node. This node includes a Computed Field, which embeds the results of a View showing the current user's flagged nodes.
  3. The user submits the new Enquiry node, and their currently flagged nodes are saved with the new node.

This works well, except I need a method to clear all their flags when they submit the Enquiry node - similar to the way your products are removed from a shopping cart once you've checked out.

Is it possible to expose an Action to the Rules module to reset all flags of a particular type created by the current user?

Thanks for your consideration.

Comments

Mark Nielsen’s picture

Status: Active » Fixed

My friend neokrish has just supplied me with the following Action code which solves this problem.

On the third line, you'll need to replace 'NAME_OF_YOUR_FLAG_HERE' with the machine name of the flag you wish to clear. This will (by design) only clear flags set by the current user.

  $items = flag_get_user_flags('node');
  $flag = flag_get_flag('NAME_OF_YOUR_FLAG_HERE');
  foreach($items['services'] as $nid => $obj) {
    $flag->flag('unflag', $nid); 
  }

With this simple code, perhaps there's no need to expose this to views?

I've not closed this issue, in case others want to comment, or review this solution.

neokrish’s picture

Thanks Mark for sharing this. I am interested to see if better solution exists and learn them :-)

One small thing that I noticed is that
NAME_OF_YOUR_FLAG_HERE should appear twice as below:

  $items = flag_get_user_flags('node');
  $flag = flag_get_flag('NAME_OF_YOUR_FLAG_HERE');
  foreach($items['NAME_OF_YOUR_FLAG_HERE'] as $nid => $obj) {
    $flag->flag('unflag', $nid);
  }
Mark Nielsen’s picture

Thanks for that correction, neokrish. Glad you spotted it!

randomuser’s picture

Sorry to hijack the thread slightly but I'm working on this kind of setup as well. A few steps behind you actually but finding the code for resetting the flags is a great bit of luck. Thanks to the both of you for that but there was one thing...

This node includes a Computed Field, which embeds the results of a View showing the current user's flagged nodes.

What was the code to embed a view in a computed field?

Thanks again.

neokrish’s picture

You might need this: views_embed_view('view_name', 'default');
for further discuss please move the discussion to forum to keep the module maintainer's sanity and I can answer you there, if needed :-)

randomuser’s picture

Ok, I made a post about my problem but no responses so far.

Mark Nielsen

Can you share the code you used in the Computed field to display view data if you get a chance.

Computed field retrieving View data - http://drupal.org/node/1080468

Mark Nielsen’s picture

Status: Fixed » Closed (fixed)

Thanks for flagging up your forum post. I've responded there (node/1080468#comment-4168442).