What are the difference? And when should you use which one?

entity_uri() is rather new to me (Drupal 7) and I see it little used. But it appears to be a lot cleaner and better performing then drupal_get_path_alias().

FYI: I want to get some background information to deal with an issue on Tagadelic.


These functions actually have not much in common. There is no question of using one or the other. You will in fact use both of them, although you will most likely not call drupal_get_path_alias() directly.

entity_uri() returns an array that contains the path and options to link to an entity. So for example for a node, you call it like this: entity_uri('node', $node) and it will return node/$node->nid. That's kinda trivial. It gets more interesting with other entity types, like private messages (where the path is messages/view/$message->thread_id#privatemsg-mid-$message->mid).

You use the returned array like this: url($uri['path'], $uri['options']) (or similarly with l()). Then, that function will call drupal_get_path_alias() internally to check if there is an alias for that internal path and will use that.

The idea is that you can generate the internal path for any entity, without any kind of special handling. A real life example is the Userpoints project. Userpoints transactions support to reference an entity by specifying type and id (Yes, Userpoints used the entity_type terminology since Drupal 5...). Previously, there was a large bunch of code with support for nodes, comments and users and a custom hook to be able to support more things. When I ported the module to Drupal 7, I was able to replace all that with a simple call to entity_uri() and it now supports all entities out of the box: userpoints_create_description().

| improve this answer | |

The 'problem' with a lot of entity-centered functions is that the entity type is a required parameter. (I ran into that before...) Also, entity_uri() requires a fully loaded entity, so I guess it may be bad for performance if you have to do a lot of entity_uri calls.

On the other hand, drupal_get_path_alias requires you to know (read: hard code) the url patterns (like node => node/[nid]) for every entity type. That might be OK for a a custom module, but it could break in a contrib module that is supposed to work with all kinds of entities.

| improve this answer | |

entity_uri() returns the URI associated with the entity passed as argument, while drupal_path_alias() returns the alias of the path passed as argument, which could also be not associated with an entity (which is what happens with URLs to administrative pages).

For example, if you have an alias for example.com/node/1 (e.g. example.com/home), the following code would set $alias to "home."

$alias = drupal_get_alias('node/1');

The following code would set $path to "node/1."

$node = node_load(1);
$uri = entity_uri('node', $node);
$path = $uri['path'];

In the latter case, you could have used "node/" . $node->nid. Using entity_uri() allows you to get the correct URI, even in the case the URI associated with nodes are changed, and the same code works for different type of entities.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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