This error appears to occur when saving a node with an attached field_collection field.

Comments

acrollet’s picture

Status: Active » Needs review
StatusFileSize
new650 bytes

Status: Needs review » Needs work

The last submitted patch, flag-notice_field_collection-1774226-1.patch, failed testing.

joachim’s picture

Status: Needs work » Postponed (maintainer needs more info)

> + 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?

TimG1’s picture

Title: incompatible with field collection - Trying to get property of non-object in flag_entity->applies_to_content_object() » Notice: Trying to get property of non-object in flag_entity->applies_to_content_object()
Version: 7.x-2.x-dev » 7.x-2.0-beta9
Priority: Major » Normal

Hello, 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.

Notice: Trying to get property of non-object in flag_entity->applies_to_content_object() (line 1388 of /sites/all/modules/contrib/flag/flag.inc).

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 node field 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

joachim’s picture

Title: Notice: Trying to get property of non-object in flag_entity->applies_to_content_object() » incompatible with field collection - Trying to get property of non-object in flag_entity->applies_to_content_object()
Version: 7.x-2.0-beta8 » 7.x-2.x-dev
Status: Postponed (maintainer needs more info) » Active

Thanks 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:

function flag_field_attach_submit($entity_type, $entity, $form, &$form_state) {
  // Put the form values in the entity so flag_field_attach_save() can find
  // them. We can't call flag() here as new entities have no id yet.
  if (isset($form_state['values']['flag'])) {
    $entity->flag = $form_state['values']['flag'];
  }
}

// .... snip

function flag_field_attach_save($entity_type, $entity) {
  list($id) = entity_extract_ids($entity_type, $entity);
  // Get the flag values we stashed in the entity in flag_field_attach_submit().
  foreach ($entity->flag as $flag_name => $state) {
    flag($state ? 'flag' : 'unflag', $flag_name, $id);
  }
}

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?

joachim’s picture

Priority: Normal » Major
joachim’s picture

Title: Notice: Trying to get property of non-object in flag_entity->applies_to_content_object() » incompatible with field collection - Trying to get property of non-object in flag_entity->applies_to_content_object()
Version: 7.x-2.0-beta9 » 7.x-2.x-dev
Priority: Normal » Major

What 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!

joachim’s picture

Version: 7.x-2.x-dev » 7.x-3.x-dev
Issue tags: +Novice

Ah 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?

joachim’s picture

Status: Active » Needs review
StatusFileSize
new1.21 KB

Could 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!!!

TimG1’s picture

Hi 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

joachim’s picture

Yup, apply to 7.x-3.x.

joachim’s picture

Though 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...

TimG1’s picture

Hi 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

joachim’s picture

Status: Needs review » Fixed

Brilliant! 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.

joachim’s picture

Version: 7.x-3.x-dev » 7.x-2.x-dev

Cherry-picked to the 7.x-2.x branch, where I'll make a 2.0 release soon!

Status: Fixed » Closed (fixed)

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

Yorgg’s picture

I'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.

joachim’s picture

Can you file a new bug for this? It's likely a slightly different problem.