Closed (fixed)
Project:
Flag
Version:
7.x-2.x-dev
Component:
Flag core
Priority:
Major
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
4 Sep 2012 at 20:25 UTC
Updated:
13 Apr 2013 at 18:50 UTC
Jump to comment: Most recent file
Comments
Comment #1
acrollet commentedComment #3
joachim commented> + if (is_object($entity)) {
This is the wrong fix. Methods shouldn't have to sniff to check their parameters are sane.
Also, saving a node with a FC is working fine for me.
Can you debug to find out what's causing the problem?
Comment #4
TimG1 commentedHello, I'm getting this error too.
For me it happens when my node has a field collection and I save my node while trying to have a value in my field collection. If I have no field collection value and save, I don't get the error.
ABOUT MY FIELD COLLECTION
My field collection has 2 fields, a text field and a entity reference field (unlimited values)
My node type is set up to have multiple values of the above field collection.
ABOUT THE ERROR
This is the exact error I get.
I have 8 flags on my node type and get this error 16 times if I enter a single value in the text field of my
nodefield collection and save.I'M RUNNING
Drupal 7.15
Flag 7.x-2.0-beta9
Field collection 7.x-1.0-beta4
Entity reference 7.x-1.0-rc3+2-dev (2012-Jun-18)
Entity API 7.x-1.0-rc3
I hope that helps.
Thanks for reading,
-Tim
Comment #5
joachim commentedThanks for the report.
We'll have to bump this to 3.x but in the meantime some investigating on 2.x shows that the problem is that the various flag methods are trying to deal with a *node* with an id that is that of the *field collection*.
In fact, if it so happened that there was a node with that ID, you'd get nodes you don't know about spookily getting flagged!! Fun!
The problem begins here:
Our code expects to come here for the node, but these also get invoked for the FC, and the code can't tell the difference.
I'm not in fact entirely sure how we fix this. We *could* make sure that $entity_type agrees with the flag being processed. But what if you have a FC on a FC? Or for that matter, anther type of inline form (with Inline entity form module, say) and it's a node in a node?
Any thoughts?
Comment #6
joachim commentedComment #7
joachim commentedWhat happens at the moment is that hook_field_attach_form() is responsible for adding the form checkbox for a flag (except on nodes, where there is special handling). This does the job of figuring out which entity type is being created or edited, and which flags apply, and then adding checkboxes to the form array.
This may be invoked multiple times if a form is for multiple entities, but that's okay, because at this stage we know which entity type and entity we're working with.
hook_field_attach_submit() is then invoked when the form is submitted, but again, once for each entity in the form.
At this point, we've no idea what we're working with. We just have $form_state['values']['flag'], which is the values of our checkboxes. Note that that probably means that if one of the embedded entities (ie, the field collection) had a flag on it too, either that wouldn't work at all, or values would get clobbered -- I don't know).
We could add some sort of identification to the form in some way that the form values can know which form it was on, but what? The form id won't do, as there is still only one. The entity type will sort of do, but if we have a field collection inside a field collection (don't laugh, I've done that for a good reason) then it breaks too.
I am starting to think that hook_field_attach_form() is the wrong approach entirely, and we should be using plain old hook_form_alter and sniffing $form['#entity_type'] (which I think should work on most entities, and for those it doesn't modules can provide glue), but then our flag checkboxes certainly won't appear on any embedded entity forms at all!
Comment #8
joachim commentedAh well a flag on the FC entity form doesn't work anyway -- the value is in $form_state['values']['field_node_fc']['und'][2]['flag']['fc_flag'], where our hook_field_attach_submit() won't spot it.
However, that does give an indication of the solution to the problem.
The FC hook_field_attach_submit() comes in with *just* the FC entity form, not the whole form. And now that's been processed, $form['flag']['#parents'] (the form element we added in hook_field_attach_form()) is now set and shows us the route to our values in $form_state['values'].
Therefore, tentatively, in hook_field_attach_submit() we should:
1. look in $form['flag'], if it exists
2. look at the #parents
3. use that to go looking in $form_state['values'] for our flag values
4. only once that all joins up should we act on it, and stash something in the $entity for the next hook to work with.
Anyone care to make a patch?
Comment #9
joachim commentedCould someone try this patch?
(Note that putting a flag checkbox on the *field collection* doesn't work -- that's a whole new bug I discovered this morning. This issue is just about getting the node to save when it has both a flag checkbox and a field collection.)Scratch that, it *does* work. Hurrah!!!Comment #10
TimG1 commentedHi joachim,
Wow, thanks for the quick work on coming up with a patch! I'll be happy to test over the next couple days. Just to be sure, I should apply this patch to the 7.x-3.x-dev version, correct?
Thanks again!
-Tim
Comment #11
joachim commentedYup, apply to 7.x-3.x.
Comment #12
joachim commentedThough actually, it should apply cleanly to 7x-2x too, which would save you the hassle of setting up a new dev site to test it...
Comment #13
TimG1 commentedHi Joachim,
I tested on 7.x-2.x-dev AND 7.x-3.x. Just did a quick couple node saves to test and the error seems to be gone on both. =D I'll continue my development on the -3.x version and let you know if it comes back or I notice other funny business.
Many thanks!
-Tim
Comment #14
joachim commentedBrilliant! Thanks for trying it out.
Issue #1774226 by joachim: Fixed flag_field_attach_submit() assuming it's the only entity on a form, causing errors with field collections.
Comment #15
joachim commentedCherry-picked to the 7.x-2.x branch, where I'll make a 2.0 release soon!
Comment #17
Yorgg commentedI'm using version 2.0 and this issue continues to show up:
Notice: Trying to get property of non-object in flag_entity->applies_to_content_object() (line 1388 of modules/flag/flag.inc)for a number of display suite / apache solr pages.I would go for 7.x-3.0 but then dlike module wouldn't work.
Comment #18
joachim commentedCan you file a new bug for this? It's likely a slightly different problem.