Drupal - How to debug EntityMalformedException?

The error:

EntityMalformedException: Missing bundle property on entity of type node.

happens, because your bundle property is malformed either on load or save, so Drupal can't find what type of bundle is it.

The logic of that Exception is:

// Explicitly fail for malformed entities missing the bundle property.
if (!isset($entity->{$info['entity keys']['bundle']}) || $entity->{$info['entity keys']['bundle']} === '') {
  // var_dump(debug_backtrace()); exit; // You may want this line to debug.
  throw new EntityMalformedException(t('Missing bundle property on entity of type @entity_type.', array('@entity_type' => $entity_type)));
}

So basically value of $info['entity keys']['bundle'] (for node it's: type) can't be found in $entity object ($node->type for the node), therefore Drupal doesn't know with what kind of entity it's dealing with. So most likely your entity is invalid (e.g. you're loading something else instead) or it is just empty ($entity is NULL).


If you haven't modified any Drupal code, this potentially can be caused by variety of things (most likely by specific Drupal module bug), such as:

  • saving invalid entity such as:

    • missing type from the node object (e.g. wrongly generated by Devel Generate?);
  • orphaned nodes in node table;

  • invalid rows in field_data_field_xxx table;
  • having orphaned nodes, caused by:
    • search view having indexed nodes which aren't in the system;
    • other modules (like Feeds) removing nodes from Drupal improperly;
  • nodes having invalid/non-existing references; for term data this can be checked by:

    drush sqlq "SELECT nid, title from node WHERE nid IN (select entity_id from  field_data_field_some_ref WHERE field_some_ref_tid not IN (select tid from taxonomy_term_data));"
    

    Where field_data_field_some_ref is data field of field_some_ref which is type of 'Term reference'.

    See: DB records not deleted for Term Reference Fields after Term is Deleted

  • having orphaned taxonomy terms (so you may use Taxonomy Orphanage);

  • entity caching issue;
  • when using Rules Link module;
  • invalid custom code (human error) such as:

    • badly implemented Entity API,
    • badly implemented CTools/Form API,
    • !empty() test on the entity variable before calling field_get_items(),
    • rendering field by invalid calls,
    • triggering functions which shouldn't be triggered on certain pages (e.g. field_view_field()),

Here is the responsible code which is thrown by Drupal core (file: common.inc):

 if (!empty($info['entity keys']['bundle'])) {
    // Explicitly fail for malformed entities missing the bundle property.
    if (!isset($entity->{$info['entity keys']['bundle']}) || $entity->{$info['entity keys']['bundle']} === '') {
      throw new EntityMalformedException(t('Missing bundle property on entity of type @entity_type.', array('@entity_type' => $entity_type)));
    }
    $bundle = $entity->{$info['entity keys']['bundle']};
  }

Debugging

If you don't recognise anything above, the easiest thing to debug that kind of error is by placing var_dump(debug_backtrace()); or dd(debug_backtrace()); (when Devel is on) before the actual throw new EntityMalformedException on the affected line in common.inc.

Note: Using dd() function from Devel will generate the debugging info to the file in your Drupal temp folder (temporary://drupal_debug.txt) with backtrace dump, otherwise could be too big and difficult to read when dumping on the screen. When using var_dump(), it's easier to call die(); after the call and check the dump in the view-source mode of the page.

If this is happening on node save, check this EntityMalformedException post at SO for more detailed instructions.


See also the following Drupal issue: #1778572 for more ideas.


Thanks to Clive comments, I solved the issue as following.

Added ddebug_backtrace() where error occured (entity_extract_ids(), line 7700 of .\includes\common.inc ) to print the function call stack.

Then looking for anything unexpected in output, I found that a pane visibility rule might be the problem.

19: ctools_entity_field_value_ctools_access_check() (Array, 2 elements)
  file (String, 81 characters ) profiles\commons\modules\contrib\ctools\plugins...
  $...['19: ctools_entity_field_value_ctools_access_check()']['file']
    profiles\commons\modules\contrib\ctools\plugins\access\entity_field_value.inc:213
  args (Array, 3 elements)
    0 (Array, 2 elements)
      field_theme (Array, 1 element)
      //...

I had applied a patch on entity_field_value.inc a few days ago just to solve a visibility rule notice... and created a test visibility rule with a field_theme condition.

Now reverting patch or removing any pane visibility rule solved current EntityMalformedException bug... Powerful ddebug_backtrace() !