EntityMalformedException in custom module. Can anyone see what I did wrong? I'm trying to create a custom module and found out that it's causing the following error message to appear:

EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7721 of ~/includes/common.inc).

if (!isset($entity-&gt;{$info['entity keys']['bundle']}) || $entity-&gt;{$info['entity keys']['bundle']} === '') {
  throw new EntityMalformedException(t('Missing bundle property on entity of type @entity_type.', array('@entity_type' =&gt; $entity_type)));
}


I'm using xdebug setup with netbeans to debug the error and found out what is causing the culprit.

I found out it was a specific field I added in my module that looks like this:

$form['base']['st-volume-pricing']['add'] = array(
'#type' =&gt; 'button',
'#value' =&gt; t('Add'),
'#name' =&gt; 'add_row',
'#limit_validation_errors' =&gt; array(
  array('price'),
  array('till-amount'),
),
'#ajax' =&gt; array(
  'callback' =&gt; 'bulk_and_user_pricing_add_volume_standard_ajax_callback',
  'wrapper' =&gt; 'error-placeholder',
  'method' =&gt; 'append',
  'effect' =&gt; 'fade',
),
'#prefix' =&gt; '&lt;td&gt;',
'#title' =&gt; t('Click this button to add a standard volume price'),
);


My debugger basically showed the following data:

$info['entity keys']['bundle'] = "type"
$entity_type = "node"

$entity stdClass        
add string  "Add"   
add_row string  "Add"   
price   string  "123"   
till-amount string  "123"


I checked my database and couldn't find any nodes without type so that shouldn't cause any problems.

I suppose I have to define the bundle in my code somewhere but I'm not that famillair with bundles yet.

The field is added to the node edit page of a product created by the ubercart module using hook_form_FORM_ID_alter() -&gt; hook_form_product_node_form_alter().

Does anyone have a clue as to what I should do to define the bundle?

Or in OOP terms, how can I make my field a class member of the ubercart product bundle (which is an extended class of the entity type base class)?
Please check Examples, specifically entity_example module to check how to define the bundle.

So for Drupal 7 it goes like:

/**
 * Implements hook_entity_info().
 *
 * This is the fundamental description of the entity.
 *
 * It provides a single entity with a single bundle and without revision
 * support.
 */
function entity_example_entity_info() {
  $info['entity_example_basic'] = array(
    // A human readable label to identify our entity.
    'label' =&gt; t('Example Basic Entity'),

    // The controller for our Entity, extending the Drupal core controller.
    'controller class' =&gt; 'EntityExampleBasicController',

    // The table for this entity defined in hook_schema()
    'base table' =&gt; 'entity_example_basic',

    // Returns the uri elements of an entity.
    'uri callback' =&gt; 'entity_example_basic_uri',

    // IF fieldable == FALSE, we can't attach fields.
    'fieldable' =&gt; TRUE,

    // entity_keys tells the controller what database fields are used for key
    // functions. It is not required if we don't have bundles or revisions.
    // Here we do not support a revision, so that entity key is omitted.
    'entity keys' =&gt; array(
      // The 'id' (basic_id here) is the unique id.
      'id' =&gt; 'basic_id' ,
      // Bundle will be determined by the 'bundle_type' field.
      'bundle' =&gt; 'bundle_type',
    ),
    'bundle keys' =&gt; array(
      'bundle' =&gt; 'bundle_type',
    ),

    // FALSE disables caching. Caching functionality is handled by Drupal core.
    'static cache' =&gt; TRUE,

    // Bundles are alternative groups of fields or configuration
    // associated with a base entity type.
    'bundles' =&gt; array(
      'first_example_bundle' =&gt; array(
        'label' =&gt; 'First example bundle',
        // 'admin' key is used by the Field UI to provide field and
        // display UI pages.
        'admin' =&gt; array(
          'path' =&gt; 'admin/structure/entity_example_basic/manage',
          'access arguments' =&gt; array('administer entity_example_basic entities'),
        ),
      ),
    ),
    // View modes allow entities to be displayed differently based on context.
    // As a demonstration we'll support "Tweaky", but we could have and support
    // multiple display modes.
    'view modes' =&gt; array(
      'tweaky' =&gt; array(
        'label' =&gt; t('Tweaky'),
        'custom settings' =&gt; FALSE,
      ),
    ),
  );

  return $info;
}


Or for simpler example, entity.api.php from Entity module which goes like:

function entity_crud_hook_entity_info() {
  $return = array(
    'entity_test' =&gt; array(
      'label' =&gt; t('Test Entity'),
      'entity class' =&gt; 'Entity',
      'controller class' =&gt; 'EntityAPIController',
      'base table' =&gt; 'entity_test',
      'module' =&gt; 'entity_test',
      'fieldable' =&gt; TRUE,
      'entity keys' =&gt; array(
        'id' =&gt; 'pid',
        'name' =&gt; 'name',
        'bundle' =&gt; 'type',
      ),
      'bundles' =&gt; array(),
    ),
  );
  foreach (entity_test_get_types() as $name =&gt; $info) {
    $return['entity_test']['bundles'][$name] = array(
      'label' =&gt; $info['label'],
    );
  }
  return $return;
}


The error EntityMalformedException which you're having it's saying that your bundle property is malformed on save, so Drupal core can't identify what type (bundle) is that.

Troubleshooting:


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("entity_type"));'

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("entity_type", array(1)); entity_get_controller("entity_type")-&gt;save(reset($entity))'


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