7.x common.inc entity_extract_ids($entity_type, $entity)

Helper function to extract id, vid, and bundle name from an entity.


$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, or NULL if $entity_type has no bundles.
42 calls to entity_extract_ids()
entity_form_submit_build_entity in includes/common.inc
Copies submitted values to entity properties for simple entity forms.
entity_uri in includes/common.inc
Returns the URI elements of an entity.
FieldBulkDeleteTestCase::_generateStubEntities in modules/field/tests/field.test
Convenience function for Field API tests.
field_attach_delete in modules/field/field.attach.inc
Delete field data for an existing entity. This deletes all revisions of field data for the entity.
field_attach_delete_revision in modules/field/field.attach.inc
Delete field data for a single revision of an existing entity. The passed entity must have a revision id attribute.

... See full list


includes/common.inc, line 7928
Common functions that many Drupal modules will need to reference.


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(


joachim’s picture

Use thus:

list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
danielb’s picture

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.

rashidkhan’s picture

I hope this example will make it clear.
(a) Suppose you want to load a single entity, you can load it by two methods. You can either use reset after entity_load or you can use entity_load_single function.

    $entity = entity_load('node', array(1));
    $entity = reset($entity);
    $single_entity = entity_load_single('node', 10);

If you know an entity type and have the entity object want to have the id, vid and bundle etc then you can use the entity_extract_ids function,

list($id, $vid, $bundle) = entity_extract_ids('node', $entity);
//if this entity is of type node.