How to find the entity specific named id? How do I map entity type to the entity specific named id, like this:

node =&gt; nid
taxonomy_term =&gt; tid
comment =&gt; cid


so that I can turn code like

 $entity-&gt;nid


into something like

$entity-&gt;{$label}


and correctly handle non-node entities.
This should do it (definitely works for all core entities, I don't have any custom entities to play with on my sandbox site I'm afraid):

$type = 'taxonomy_term';

// Get the entity type info
$info = entity_get_info($type);

// Grab the id key
$key = $info['entity keys']['id'];

// $key contains 'tid'


I'm pretty positive this will work across any entity type though, according to hook_entity_info() the id property of entity keys is required:


  Every entity object passed to the Field API must have this property and its value must be numeric.