Closed (fixed)
Project:
Flag
Version:
7.x-3.x-dev
Component:
Rules integration
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
13 Feb 2012 at 21:05 UTC
Updated:
23 May 2013 at 07:00 UTC
Jump to comment: Most recent file
Comments
Comment #1
joelstein commentedHere's the one-line patch to solve this bug. Thanks!
Comment #2
joelstein commentedUpon further investigation, it seems that if in a certain situation the Rules "trim a flag" action does not receive the $flagging_user, but instead receives a FALSE value. Here's how to reproduce this.
1) Create a vanilla installation of Drupal, Flag, Session API & Rules.
2) Create an flag which is flaggable by anonymous users.
3) Setup a Rule to respond to the event "A node has been flagged, under ___" (the name of your flag).
4) In that rule, add a "Trim a flag" action, and trim to 1 for the flagging user. (This is supposed to keep it so only one of this flag per user is ever available.)
Now, go flag something as an anonymous user. You'll get an error, "Notice: Trying to get property of non-object in flag_trim_flag() (line 1015 of sites/all/modules/flag/flag.module)." If you use Devel to debug the $flagging_user variable in flag_rules_action_trim(), you'll see that it's not the expected user object, but a FALSE value.
Note that this doesn't happen for authenticated users, or if you trim a flag in response to other types of Rules events.
I'm not sure why this is happening, but it seems that somewhere in the Rules execution flow, the anonymous $flagging_user object is lost.
Solutions include figuring out why that user is lost, or simply checking if the $flagging_user is FALSE, and then setting it to the current anonymous user, since we know that it only happens for anonymous users.
The attached patch proposes the second solution, since I don't know how to go about troubleshooting the Rules execution flow.
Comment #3
joelstein commentedWell, it seems that both of these patches are needed to make trimming a flag possible for both anonymous and authenticated users, in such a way that trimming a flag for one user doesn't trim it for multiple users (as is certainly the case when trimming for an anonymous user, especially).
In summary, this patch does two things:
1) It accounts for the session ID (sid) of the user when trimming a flag.
2) It makes sure that there's a user object when trimming a flag via Rules, for an anonymous user.
Comment #4
mattcasey commentedThere's a similar issue posted for 6.x: http://drupal.org/node/1134384.
Also, for what it's worth, I have the same setup in #2 using 6.x-beta2, but I don't get those errors.
Comment #5
joachim commentedThanks for the patch.
Here's a quick eyeball review:
I'd like some code comments to explain what's being done here please.
Has anyone had any luck in tracking the cause for this down in Rules?
Comment #6
joachim commentedComment #7
joelstein commentedThis has been updated for 7.x-3.x-dev, and a comment was added to help clarify why we need a condition for the SID.
Comment #8
joachim commentedTests pass, but that is no indication -- we have no test coverage of Rules at all.
Could someone try this out on their Rules set up and report back?
I'll commit this when it's set to RTBC :)
Comment #9
nlisgo commentedI've implemented patch #7 manually on alpha4 and it works in rules for me.
Comment #10
joachim commentedI'll take that as a RTBC. Thanks! :)
Comment #11
joachim commentedCommitted. Thanks!
git commit -m "Issue #1439168 by joelstein: Fixed flag trim removing flaggings for the anonymous user." --author="joelstein "