Get the bundle of a Drupal 7 entity


Drupal 7's EntityAPI allows entities to be divided into different "bundles" based on their properties. Unfortunately, there's no way to easily determine what bundle a particular concrete entity object belongs to. This helper function appears to work for the entity types that ship with core.

function _entity_get_bundle($entity, $entity_type) {
  $info = entity_get_info($entity_type);
  if (empty($info['entity keys']['bundle'])) {
    return $entity_type;
  } else {
    return $entity->{$info['entity keys']['bundle']};

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.