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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Damien Tournoud’s picture

Peter, this is a field that references an invalid entity? If so, could we add a comment that says precisely that?

das-peter’s picture

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

  • Can we fix this issue by changing the code to this:
    // If an entity is found but isn't wrapped in an array yet do this now.
    if (!empty(entities) && !is_array($entities)) {
          $entities = array($entities);
        }
    

    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.

  • Stay with the current patch and add a comment e.g. :
    // 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.

das-peter’s picture

As 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 :)

fago’s picture

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

    $values->_field_data[$this->field_alias]['entity_type'] = $this->entity_type;
    // We need special handling for lists of entities as the base.
    $entities = EntityFieldHandlerHelper::get_value($this, $values, 'entity object');

This comment sounds deprecated to me.

das-peter’s picture

@fago Thanks for the feedback

The solution basically makes sense, I wonder though whether this shouldn't happen somewhere else so it applies to all field handlers.

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.

Peter, this is a field that references an invalid entity? If so, could we add a comment that says precisely that?

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.

fago’s picture

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

das-peter’s picture

Successfully tested with my actual case.

fago’s picture

Status: Needs review » Fixed

Committed.

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

Updated issue summary.