Get Entity Page URI from Entity and Entity Type in Drupal 7

Recently I encountered a situation with a contributed module which need a bug fix. Fixing that required to identify URI of an entity with entity type given. As we all know, most of the entities will have a 'view' page. For nodes, it will be node/<nid> (where nid is the id of that node). But it will not be of same pattern for other entity types. For example, URIs of Commerce Orders are of the form admin/commerce/orders/<order-id>.

Then how you will identify the URI of given entity along with entity type specified? You may required to identify this usually when you are writing a module that should be capable of handling any entities. Luckily Drupal provides a built-in function to identify the URI of any entity. It is entity_uri(). This function receives entity type and entity object as parameters and return an array containing path value keyed by 'path' and other options keyed by 'options'.

$uri = entity_uri($entity_type, $entity);
// This variable will be holding the URI of given entity.
$entity_uri = $uri['path'];

Remember, not all entities might have implemented 'uri callback' while defining the entity type. If an entity type does not define a 'uri callback', then return value of above function will be NULL.