Problem/Motivation
I've got following error (WoD) when I created a search_api view:
EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (Zeile 7434 von drupal/includes/common.inc).
While debugging I discovered that the trigger was that one of the result items had an entity_reference field without a referenced entity. While this is perfectly valid for the field the code in entity_views_handler_field_field::get_items()
fails without a referenced entity.
Proposed resolution
Added a check to loop to ensure the entity handling only takes place if there's actually an entity.
Remaining tasks
Decide which approach to use: #1348122-2: Avoid EntityMalformedException: Missing bundle property on entity of type...
Review attached patch.
User interface changes
none.
API changes
none.
Comment | File | Size | Author |
---|---|---|---|
#6 | entity_views_field_multiple.patch | 764 bytes | fago |
#3 | entity-avoid-EntityMalformedException.patch-1348122-3.patch | 1.11 KB | das-peter |
entity-avoid-EntityMalformedException.patch | 1.97 KB | das-peter | |
Comments
Comment #1
Damien Tournoud CreditAttribution: Damien Tournoud commentedPeter, this is a field that references an invalid entity? If so, could we add a comment that says precisely that?
Comment #2
das-peter CreditAttribution: das-peter commentedAs discussed with Damien via IRC I tried to figure out what happens if the referenced entity is invalid e.g. non existent id or if there are multiple items of which one is invalid.
Unfortunately the behaviour is only triggered if the field is used as relationship - which currently supports field cardinality 1.
Now the question the discussion with Damien was about is still open:
Advantage: No iteration at all if there's nothing valid to iterate over -> faster.
Disadvantage: Iteration would still fail if an array with an invalid item is returned as result.
// If there's no or an invalid entity referenced, don't deal with it.
Advantage: Checks every singly item in the array if it can be handled.
Disadvantage: Some overhead because even if the entity return isn't a valid array an iteration is started.
I personally would stick with the current approach because it's the most failure-tolerant. And in my eyes the overhead is still acceptable.
Comment #3
das-peter CreditAttribution: das-peter commentedAs often if you've multiple choices, it's wise to choose the one that's not listed.
Here we go with the idea DamZ provided via IRC - thanks!
Advantages Checks every array item and is performance because a built-in array function is used :)
Comment #4
fagoThe solution basically makes sense, I wonder though whether this shouldn't happen somewhere else so it applies to all field handlers.
>Peter, this is a field that references an invalid entity? If so, could we add a comment that says precisely that?
Yep, we should do so. Also we should get a review from drunken monkey on that.
Kind of unrelated:
This comment sounds deprecated to me.
Comment #5
das-peter CreditAttribution: das-peter commented@fago Thanks for the feedback
I'm not experienced enough with views / entity to have really an idea, but for me it seems perfectly valid to get an empty (the default) value or even an invalid id as long as this reflects the field content.
I think it's actually the job of
entity_views_handler_field_field::get_items()
to make sure depending code is able to work with the results.May I ask you to provide the necessary comment? Because, while it's likely I'm able to please requests for code changes, it's nearly impossible to find the correct phrasing in comments to please the requester.
And I'd like to keep the process as efficient as possible. :)
I'll remove the deprecated comment on the next patch re-roll.
Comment #6
fagook, talked to drunken monkey. Looks like this should be covered already in the general case. So the issue might be field handler specific. From looking at the code, the following patch could fix it. Please test.
If it doesn't fix it, please debug $entities and post the output here.
Comment #7
das-peter CreditAttribution: das-peter commentedSuccessfully tested with my actual case.
Comment #8
fagoCommitted.
Comment #9.0
(not verified) CreditAttribution: commentedUpdated issue summary.