As mentioned by eaton in http://forrst.com/posts/Get_the_bundle_of_a_Drupal_7_entity-0xW, this would be a very useful helper to have in Entity API:

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']};
  }
}

Comments

joachim’s picture

... except the params should be other way round to match all the other functions that take ($entity_type, $entity), and the else shouldn't be coddled...

fago’s picture

Well, there is entity_extract_ids() in core..

list($id, $vid, $bundle) = entity_extract_ids($type, $entity);
fago’s picture

Status: Active » Fixed

Still, for classed entity objects a bundle() helper method was missing. I've added one, as we've in d8 core now.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.