diff --git a/includes/common.inc b/includes/common.inc index 1c1b76ee7c7ff4ea668e830a8a9659023072b2f9..481edae61bd94e5dbf2af30b163041f782c88f69 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -7373,12 +7373,24 @@ function entity_info_cache_clear() { */ function entity_extract_ids($entity_type, $entity) { $info = entity_get_info($entity_type); + // Objects being created might not have id/vid yet. $id = isset($entity->{$info['entity keys']['id']}) ? $entity->{$info['entity keys']['id']} : NULL; $vid = ($info['entity keys']['revision'] && isset($entity->{$info['entity keys']['revision']})) ? $entity->{$info['entity keys']['revision']} : NULL; - // If no bundle key provided, then we assume a single bundle, named after the - // entity type. - $bundle = $info['entity keys']['bundle'] ? $entity->{$info['entity keys']['bundle']} : $entity_type; + + 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']}; + } + else { + // The entity type provides no bundle key: assume a single bundle, named + // after the entity type. + $bundle = $entity_type; + } + return array($id, $vid, $bundle); } diff --git a/includes/entity.inc b/includes/entity.inc index e3bb366aba8d5fc2cc80e835fcd1125ee616c396..9e2eff4f285dac13deb4c0c71a2c1f0d17712b62 100644 --- a/includes/entity.inc +++ b/includes/entity.inc @@ -1327,3 +1327,8 @@ public function addCondition(SelectQuery $select_query, $sql_field, $condition, } } + +/** + * Exception thrown when a malformed entity is passed. + */ +class EntityMalformedException extends Exception { }