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'])) { $bundle = $entity->{$info['entity keys']['bundle']}; // Explicitly fail for malformed entites missing the bundle property. if (is_null($bundle)) { $funcs = array(); $backtrace = debug_backtrace(); $short_backtrace = array_slice($backtrace, 0, 10); foreach ($short_backtrace as $info) { $funcs[] = $info['function']; } if (count($backtrace) > 10) { $funcs[] = '[...]'; } throw new Exception(t('Encountered an entity without a bundle property (entity type: @entity_type, callstack: @callstack).', array('@entity_type' => $entity_type, '@callstack' => implode(', ', $funcs)))); } } 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); }