Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The flag_get_user_flag_counts() does not work correctly for anonymous users because it counts all database entries per user id. But the user id of all anonymous users is 0. For anonymous users the sid provided by session_api needs to be considered.
Patch follows ...
Comments
Comment #1
mkalkbrennerAdded patch.
Comment #2
joachim CreditAttribution: joachim commentedHmm this makes sense but the problem is that it would be an API Change.
Comment #3
mkalkbrennerFrom my point of view it's rather a bug fix than an API change. The flag module itself does not use this call. It's only part of flag's API to be used by different modules (or rules). Therefor it should deliver what is documented: "Get the user's flag count."
Currently it's like "Get the authenticated user's flag count or the sum of the flag counts of all anonymous users." That second one doesn't make sense.
Comment #4
ciss CreditAttribution: ciss commentedI agree that this rather seems to be a bug than a feature. While user 0 is technically a user, I can't think of a common use case where counting the flags of all anonymous users vs the current anonymous user is preferrable.
If the API change (and I agree that it is one, since it changes the function result) should turn out to be a blocker on this issue, maybe we can introduce a third argument like $count_anon_by_session (defaulting to false)?
Btw, patch works well for Flag 3.5 + 9 commits (revision c0f427e).
Comment #5
joachim CreditAttribution: joachim commentedComment #6
joachim CreditAttribution: joachim commented> Therefor it should deliver what is documented: "Get the user's flag count."
Currently it's like "Get the authenticated user's flag count or the sum of the flag counts of all anonymous users." That second one doesn't make sens
On the one hand, I see what you mean. On the other, 'the anonymous user', uid 0, is a single Drupal user, and so in that sense we are doing what the documentation says.
Thing is, we've no idea what people are using it for in their custom code.
I think the best way to tackle this is to add an optional parameter to the function, eg $use_anon_sid, so we don't change existing behaviour.
Comment #7
gaurav.bajpai CreditAttribution: gaurav.bajpai as a volunteer and at Faichi Solutions Pvt Ltd commentedComment #8
gaurav.bajpai CreditAttribution: gaurav.bajpai as a volunteer and at Faichi Solutions Pvt Ltd commentedHi All,
Please check attached patch with optional parameter to the function.
Thanks,
Gaurav
Comment #10
gaurav.bajpai CreditAttribution: gaurav.bajpai as a volunteer and at Faichi Solutions Pvt Ltd commentedComment #11
gaurav.bajpai CreditAttribution: gaurav.bajpai as a volunteer and at Faichi Solutions Pvt Ltd commentedAttached wrong file.
Comment #12
joachim CreditAttribution: joachim commentedThanks for working on this. Looks pretty good, just a couple of things:
New param needs documentation.
Also, this is a boolean, so default should be FALSE.
Comment #13
gaurav.bajpai CreditAttribution: gaurav.bajpai as a volunteer and at Faichi Solutions Pvt Ltd commentedComment #14
gaurav.bajpai CreditAttribution: gaurav.bajpai as a volunteer and at Faichi Solutions Pvt Ltd commentedHi joachim,
Please find updated patch with given suggestions.
Thanks,
Gaurav