EntityMalformedException: Missing bundle property on entity of type student. in entity_extract_ids()

12,983

Solution 1

This issue - 'Missing bundle property on entity of type file error' - is probably what's causing your problem. A quick check to confirm this is to follow the steps taken in comment #5, as copied below:

I have the same issue which is obviously caused by a missing entity-type-property in an entity-object. I took a look into my database and discovered that the table "file_managed" has a field "type". I filled in the type of the entity object in question and it works. Obviously this is not a real solution (updating the database manually), because there exist hundreds of entries...

Solution 2

The error is thrown by Drupal core common.inc file with the following condition:

  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']};
  }

So it is explicitly failing if your bundle property is malformed on save, because it can't identify what type (bundle) is that.

There are many reason that this could fail, so you'll have to analyse it first.

(If you don't have drush, you may run the PHP code in /devel/php if Devel module is enabled).

  1. Check what's the value of bundle in entity keys section of array defined in entity information array via:

    drush eval 'print_r(entity_get_info("student"));'
    
  2. If it's type, then that means your entity which you're trying to save, it's missing type attribute.

The common mistake is usually to load the entity via entity_load() and trying to re-save it, but remember that return value is an array of entity objects indexed by their ids.

So the simple test to verify if your entity re-saving works correctly, it would be:

drush eval '$entity = entity_load("student", array(1)); entity_get_controller("student")->save(reset($entity))'

where student is your entity_type and 1 is your entity id.

If it's still doesn't work, clear the caches (entity_info_cache_clear()) or temporary edit your common.inc and dump (e.g. var_dump($entity); your $entity just before line with throw, and check what's passed into the function and investigate further why your bundle type is missing.


For further possibilities and details, check: How to debug EntityMalformedException? at DO

Solution 3

In my case, the problem was when I tried to save a node of one type with nid of another. I just needed to check if the nid type was actuallly what the type of the node being saved/updated.

Share:
12,983
tunghk_54
Author by

tunghk_54

Updated on June 04, 2022

Comments

  • tunghk_54
    tunghk_54 almost 2 years

    I create simple module named "student" , this module generate new entity is "student" and this is my code Download here

    but when I add new student , I get an error message like that:

    EntityMalformedException: Missing bundle property on entity of type student. in entity_extract_ids() (line 7501 of C:\AppServ\www\drupal-7.12\includes\common.inc).

    I look through all of my code but I can't find out some thing, Thank alot!

  • Maria Ioannidou
    Maria Ioannidou about 8 years
    For me drush eval 'print_r(entity_get_info("student"));' did not work. I had to use drush eval "print_r(entity_get_info('student'));"
  • Jason
    Jason about 7 years
    This was perfect, thank you for readily pointing out the issue.