5

I understand that entity_uri() generates a uri for a profied entity

http://api.drupal.org/api/drupal/includes--common.inc/function/entity_uri

However, is it possible to go the other way? is there a function in drupal to pass a system uri and see if it's an entity? and then return the entity object?

  • The only function that returns an object from a path is menu_get_object(), but it requires that you know the type of the object being returned, and where in the path the object ID is put. It is also a generic function that could return an object that is not an entity. – kiamlaluno Feb 3 '12 at 16:27
2

I'm afraid there's nothing (existing at least) that will do that, the URI function is provided by the module implementing the entity (in hook_entity_info()) and the method within that function is completely transparent to Drupal...the core entity module doesn't give modules the chance to implement a function that reverses the process, nor does it store how the conversion was made.

Because of this I don't think a fully generic function is possible, however you could do it on a type-by-type basis:

$uri = entity_uri('node', $entity);
if (preg_match('/node\/([0-9+])/', $uri, $matches)) {
  $entity2 = entity_load('node', array($matches[1]));
}

You could then extract that logic to build a slightly more generic function based on the known attributes of your Drupal installation...it's not a pretty way to do it though, I'd love to hear a different solution to this.

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.