11

I need to identify bundle of an entity from its type and id.

For example, if the type is 'node' and id is 7, I may get 'page' as bundle.

Thanks

4 Answers 4

11

Use entity_load to load the entity as mentioned by @Nikhil.

Then use entity_extract_ids, to extract the bundle. Example:

$entity = entity_load('node', array('7'));

list(, , $bundle) = entity_extract_ids('node', $entity);

echo "Bundle name is : " . $bundle;

The function entity_extract_ids returns a numerically indexed array (not a hash table) containing the 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.

where, 0, 1, and 2 are array indices. However, the approach used by core and contrib modules is to use the list() function.

3
  • I think it is not good to depend on array numeric index.
    – Junaid
    Mar 19, 2013 at 9:18
  • 3
    @Junaid That index is predictable so it's fine. This is how Drupal core handles this task. If the 'magic number' bothers you just use language features: list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
    – Clive
    Mar 19, 2013 at 9:29
  • @Junaid You can depend on the numeric index, when you are sure they won't change. See the edit for the explanation of why I used the numeric index. It is because the API function is designed that way. Also, the solution is not dependent of the entity module ;-)
    – AjitS
    Mar 19, 2013 at 13:18
8

Here is the cleanest one that I got from Organic groups module code.

$wrapper = entity_metadata_wrapper($entity_type, $entity_id);
$bundle = $wrapper->getBundle();

For example I can call it like:

$wrapper = entity_metadata_wrapper('node', 1);
$bundle = $wrapper->getBundle();

Edit: As @Clive pointed out, above method depends on entity module.

Here is another method that does not depend on any extra module. It is explained here https://forrst.com/posts/Get_the_bundle_of_a_Drupal_7_entity-0xW

Copy pasting that method:

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']};
  }
}
6
  • What is $group_type, $gid ? and whats the value in $bundle ?
    – niksmac
    Mar 19, 2013 at 9:24
  • Sorry, I forgot to use proper parameter names. It is corrected now.
    – Junaid
    Mar 19, 2013 at 9:27
  • You can do the same for entity_load, by the way how its cleanest ?
    – niksmac
    Mar 19, 2013 at 9:27
  • 1
    Personally I don't think entity_metadata_wrapper() is cleaner than the entity_extract_ids() method. Plus you rely on the entity module being installed...
    – Clive
    Mar 19, 2013 at 9:29
  • It's not necessarily cleaner, but it's a valid alternative, especially if you're using metadata wrappers anyway. Maybe @develkar or Junaid could edit their answer to include both of the alternatives ? (Junaid, maybe you could add links to the Entity module and to the page about entity_metadata_wrappers too?) Mar 19, 2013 at 9:48
3

Try entity_load

Load entities from the database.

The entities are stored in a static memory cache, and will not require database access if loaded again during the same page request.

Parameters

$entity_type: The entity type to load, e.g. node or user.

$ids: An array of entity IDs, or FALSE to load all entities.

$conditions: (deprecated) An associative array of conditions on the base table, where the keys are the database fields and the values are the values those fields must have. Instead, it is preferable to use EntityFieldQuery to retrieve a list of entity IDs loadable by this function.

$reset: Whether to reset the internal cache for the requested entity type.

Return value

An array of entity objects indexed by their ids. When no results are found, an empty array is returned.

3

If you want a very lightweight way, performance-wise, to get the node type from node id - you can use a single select query on the node table.

$type = db_query("SELECT type FROM {node} WHERE nid = :nid", 
                  array(':nid' => $nid))->fetchField();

That way, you don't need to invoke the entire entity hook system, with entity_load etc..

It's a single select query using the indexed node id.

2
  • we are not playing with only nodes here.
    – Junaid
    May 8, 2014 at 11:52
  • OP wrote "For example, if the type is 'node' and id is 7, I may get 'page' as bundle." hence the node based example answer. May 4, 2015 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.