Currently it is - as far as I understand - not possible to calculate with flag counts. There is an event raised when a certain number of flags is set, which doesn't allow for much accuracy.

I think it should be possible to fetch a flag's overall count as well as the count of flags set for a certain entity.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Tafa’s picture

It would be great to be able use such features indeed as i am in a similar situation trying to use the flag count with rules.

Anonymous’s picture

Category: feature » bug

Another way to fix this issue would be to correct the project page. I.e. replace "Full Rules module integration" with "Partial Rules module integration".

Full integration means to provide all the relevant module data to Rules. Currently, Rules support is limited to toggeling flags and react on flagging events with certain thresholds.

Anonymous’s picture

Version: 7.x-2.0-beta6 » 7.x-2.x-dev

I ran into this issue again (for another use-case). Had a look at the code, but my knowledge of Rules API is limitted. Please somebody assist. I can't believe it would be a great effort to add flag counts support to rules.

Scyther’s picture

Title: access flag counts in rules » Add Rules action - get flag count
Version: 7.x-2.x-dev » 7.x-3.x-dev
Assigned: Unassigned » Scyther
Category: bug » feature

I will make a new Rules action so you can get the flag count.

Start with the 7.x-3.x and then backport.

shabana.navas’s picture

Assigned: Scyther » shabana.navas
Status: Active » Needs review
FileSize
4.47 KB

I have added the rules action to get the overall flag count and to get the flag count for a specific entity to the 7.x-3.x version. Please test it out.

joachim’s picture

Status: Needs review » Needs work

Thanks for working on these Rules issues :)

Quick eyeball review:

+++ b/flag.module
@@ -1644,6 +1644,37 @@ function _flag_link_type_descriptions() {
+ * @return $counts
+ *   The flag count with the flag name and entity type as the array key.
+ */

The @return documentation shouldn't have a variable name.

+++ b/flag.module
@@ -1644,6 +1644,37 @@ function _flag_link_type_descriptions() {
+    $counts[$flag->name][$entity_type] =  $result; ¶

Remember to trim whitespace :

+++ b/flag.module
@@ -1651,8 +1682,8 @@ function _flag_link_type_descriptions() {
- * @return $flags
- *   An array of the structure [name] => [number of flags].
+ * @return $counts
+ *   The flag count with the entity type and id as array keys.

Are we changing the return of this, or is it just diff being weird?

shabana.navas’s picture

I have attached the updated patch.

Are we changing the return of this, or is it just diff being weird?

Actually, the return type for that function was wrong. It wasn't returning an array of flags, so I changed the comment.

shabana.navas’s picture

Forgot to change the comment no. on the patch title. Here's the patch again.

oystercrackher’s picture

@shabana

If I have flag for "Like Node" which enables users to like various nodes. Will the patch enable me get the number of nodes a user has flagged as like?

I am trying to do the following and want to see if this patch is designed to help:

Event- Node is flagged as "Like Node"
Condition- User has role
Action- Fetch Content Flagged By User
Action- If flag count < 10 (Your patch??) I'm using Conditional Rules module as well.
Action- Grant Points To User

Any help is greatly appreciated.

Thanks

shabana.navas’s picture

@oystercracker, I haven't tested this in conjunction with the Conditional Rules module, however, I believe it should work. Basically, with the 'Fetch overall flag count' you can get the total count of the 'Like node' flag and then, you can set up your condition for it to be for a certain user. I am not sure how Conditional Rules work, as I haven't used it yet, but I guess you can use it to add the condition, only if the 'overall_flag_count' field is less than 10.

Hope this helps.

oystercrackher’s picture

@Shabana

The patch applied successfully without errors and your options do appear but do not work for my use case above. For my case, I would need to be able to get the user's flag count for a certain flag then be able to do a comparison of the count. Do you know how I might implement an action into rules to get the user's flag count for a certain flag?

Thanks in advance,

shabana.navas’s picture

Status: Needs work » Needs review
FileSize
6.12 KB

I have now incorporated a new rule action to get the user's flag count as well. Thanks for bringing to light the use case. As @oystercrackher just did, I hope a lot more users can post features that they would like to be added to Flags, so that it can be better integrated with Rules. My site only implements Rules with Flags for simple use cases.

Consequently, it would be very helpful if you guys can post Rules features which you believe should definitely be part of Flags 7.x-3.x.

SGhosh’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #12 tested and confirmed.

My only feedback for this would be -
* Fetch overall flag count - the name is not very self-explanatory: it actual fetches the total no. of nodes that are flagged under a given flagged. So content A flagged by multiple users, yet this action will return 1.
My feedback: Change name to Fetch overall flag count for entity/node

shabana.navas’s picture

Actually, I think you've misunderstood the function. 'Fetch overall flag count' gets the total count of items flagged within a flag. It is not just the count of the total number of nodes that are flagged, but it grabs every entity type flagged within a flag, including nodes. So, I think the name is appropriate.

shabana.navas’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 7.x-3.x-dev.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.