| 7 common.inc | entity_extract_ids($entity_type, $entity) |
| 8 entity.module | entity_extract_ids($entity_type, $entity) |
Helper function to extract id, vid, and bundle name from an entity.
Parameters
$entity_type: The entity type; e.g. 'node' or 'user'.
$entity: The entity from which to extract values.
Return value
A numerically indexed array (not a hash table) containing these elements: 0: primary id of the entity 1: revision id of the entity, or NULL if $entity_type is not versioned 2: bundle name of the entity
41 calls to entity_extract_ids()
File
- includes/
common.inc, line 7530 - Common functions that many Drupal modules will need to reference.
Code
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 (!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);
}
Login or register to post comments
Comments
Use thus: list($id, $vid,
Use thus:
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);MIssing bundle property error
This is often used after entity_load(). Note that unlike node_load() which returns an object, entity_load() returns an array of objects - even if you only asked for one. Be sure to get the entity out with something like reset() before passing the result to this function or you will get the unhelpful "Missing bundle property" error.