In some cases, flag_get_content_flags() causes a notice:
Notice: Undefined offset: 30 in flag_get_content_flags() (regel 1753 van /www/sites/all/modules/flag/flag.module).
Situation:
In a custom module, I want to get information about the flags on a number of nodes. I'm using an individual flag that can be set and unset by all logged-in users. Some nodes are flagged, some are not; in my test site, node 20 is flagged, node 30 is not. The code below is enough to trigger the notice. (With Devel installed you can test this on example.com/devel/php.)
$flag20 = flag_get_content_flags('node', 20, 'myflag');
$flag30 = flag_get_content_flags('node', 30, 'myflag');
It's interesting that this only occurs after running flag_get_content_flags for a flagged node. Just calling $flag30 = flag_get_content_flags('node', 30, 'myflag'); does not result in a notice.
Comments
Comment #1
marcvangendI don't know if this is the best way to solve it, but this is the temporary quick-fix I'm using right now:
I'd be happy to roll a patch if you think this is the right direction.
Comment #2
arnoldbird commentedTo avoid hacking the module, I did this in my module...
Though that adds a query that wouldn't be necessary with marcvangend's fix.
Comment #3
joachim commentedCould someone pinpoint how the second call for node 30 causes the error when the first does not?
I think it would be more elegant to prefill the $entity_flags[$entity_type][$entity_id] array with 'users' and 'flags' and empty arrays before the results are fetched into it, but I'm not entirely sure what's going wrong.
Comment #4
naxoc commentedI have the same problem no matter what node I call it on. I have flags in features (ie. not it the database), so the query to the database will never give me my flag I think?
Comment #5
joachim commentedFlags in features get saved to the DB immediately. Check if yours is there!
Comment #6
naxoc commentedWoops, yeah sorry. Was too fast there. Not features at all.
But the query in
flag_get_content_flags()returns nothing for me - I guess that means the user or node has no flags. The return statement is assuming that that there is always data.Comment #7
joachim commentedAh yes, you're right, that's the problem.
The foreach $result loop assumes there's going to be data, and if there isn't, $entity_flags[$entity_type][$entity_id] will not be set.
Comment #8
socketwench commentedIs the function still called flag_get_content_flags() in 3.x? Or should this issue be tagged 2.x?
Comment #9
joachim commentedIt's flag_get_entity_flags() in 3x, but it has the same problem.
Comment #10
naxoc commentedHere are patches for both versions.
Comment #12
naxoc commentedWoops. Didn't realize there was a testbot here. That is super cool. Patch for 7.2.x from #10 renamed so the tests will not be run on the wrong branch.
Comment #13
joachim commentedShould we not rather prepare $entity_flags[$entity_type][$entity_id] as an empty array() just before the foreach ($result)?
That way, the query that returns no results won't get run a second time because the empty array goes in the statically cached $entity_flags.
Comment #14
naxoc commentedThat makes a lot of sense. New patch. I return array() rather than $entity_flags[$entity_type][$entity_id] for readability even though the value would be the same.
Comment #15
socketwench commentedCan you rewrite the ternary if into a standard if? It's a really long line, and the standard syntax would be a bit more readable.
(I swear I've asked this in another issue somewhere...)
Comment #16
naxoc commentedAgreed. I like the ternary returns, but not when they get that long. I rewrote them.
Now I am just unhappy with the docblock for the function.. I am not sure about the wording for returning an empty array.
Comment #17
joachim commentedI don't quite follow what the @return is telling me as it is. (admittedly it's the end of a long day.)
I'm happy for it to be improved as part of a follow-on docs issue, and if you felt like tackling that, that would be great :)
> (I swear I've asked this in another issue somewhere...)
Yes, you did. Quite rightly so in both cases IMO. socketwench, ternary-killer!!! :D
Issue #1351124 by naxoc: Fixed undefined index in flag_get_entity_flags() when the given entity has no flaggings.
Comment #18
joachim commentedComment #19
naxoc commentedHere is a short doc-update to the 7.x.3.x branch. I am not a great doc-writer :)
Comment #20
naxoc commentedAaand here is the patch.
Comment #21
naxoc commentedAnd here is a backport including the small doc-line.
Comment #22
joachim commentedCommitted the docs fix and the 2.x fix too.
Comment #23
joachim commentedUntested backport patch.
Comment #24
nafmarcus commentedAny chance of getting quick instructions on applying this patch for Drupal 6.0 and 7.0? Just looking for 2 lines, where to put the file and the command use to apply the patch.
Comment #25
joachim commented*sigh*
I keep having to say to people: try googling 'drupal apply patch'.
BTW, also this patch is for 6.x-2.x only. The issue version tells you that. You can see from earlier comments that it's already been committed to 7.
Comment #26
naxoc commentedSorry to bump the version to back to 7.x.3.x, but i realized that my patches for this issue does not solve the problem in all situations.
I now have a use case where, on the same page, I had two flags potentially active. If one of them had flagged entites I would get an undefined index error because when I ask for the other flag there is no check.
So, to reproduce: have two flags on a entity and have one be flagged and one be unflagged.
Patch attached.
Comment #27
joachim commentedCommitted to 7-3.
Comment #29
joachim commentedOops. Do bits of the last patch need backporting?
Comment #30
nafmarcus commentedTo answer you question from 3 months ago, yes, I seem to need the backport for 7x2x.
Flags 3.0 isn't out yet and I'm having the problem.
I tried the patch from #23 and I still have the problem. Is that because that patch isn't complete?
Also, could you explain when this error happens?
You wrote "
but what's the situation that causes this?
Thanks,
Comment #31
nafmarcus commentedAlso, when I try to apply the patch with patch < ./1351124-21.7x2x.patch
in the /flag directory, I get:
"Reversed (or previously applied) patch detected! Assume -R? [n]"
I haven't applied any patches as far as I know.
Comment #32
corvus_ch commentedI am a little bit confused. As far as I can tell the patch in comment #26 was committed in the commits #61edfa2 #bc81820 to both 2.x and 3.x month ago. So my question is. What needs to be ported here?
By my understanding this is fixed.
Comment #33
joachim commentedNo, that was the earlier patch -- compare the date on the git commit and the date on the comment.
#26 contains an additional fix, which from a cursory read of the comments looks like it was only applied to 7--3.
So the situation is:
- 7-3: got original patch + fixup patch. All done.
- 7-2: got original patch backport. Fixup patch needs backporting -- this is where work should be done now.
- 6-2: got nothing. original patch is sitting at #23, but would need the fixup adding to it, as we should do them both in one go. This is where we'll work after 7-2 is done.
Comment #34
corvus_ch commentedThanks for clarification.
Comment #35
blueminds commentedPlease see attached backport to 7.x-2.x
Comment #36
corvus_ch commentedThe patch looks fine and I can confirm that the notice does not show up with this patch applied.
Thanks.
Comment #37
joachim commentedCommitted to 7x2x. Thanks!
git commit -m "Issue #1351124 by naxoc, blueminds: (Follow-up) Fixed undefined index in flag_get_entity_flags() when the given entity has no flaggings." --author="divamys
"