37

I know I can load a node by its title with the node_load() function. Is it possible to load a node by its URL alias?

56

You want to investigate menu_get_object and drupal_lookup_path.

Something like this would do what you want:

$alias = "some/path/to/a/page";
$path = drupal_lookup_path("source", $alias);
$node = menu_get_object("node", 1, $path);
  • Also, to dynamically grab url alias do next: $alias = $_SERVER['REQUEST_URI']; $alias = substr($alias, 1); // to remove "/" from beginning of string – Eugene Pavlov Jul 21 '16 at 20:55
  • @EugenePavlov - $alias = drupal_get_path_alias(); should be it. – leymannx Apr 5 '17 at 10:57
0

For somebody looking to find how to do this in Drupal 8 here is the corresponding question and answer

How can I get the nid of a node from the page alias?

use \Drupal\Core\Url;

$alias = \Drupal::service('path.alias_manager')->getPathByAlias('/etapes-de-la-vie');
$params = Url::fromUri("internal:" . $alias)->getRouteParameters();
$entity_type = key($params);
$node = \Drupal::entityTypeManager()->getStorage($entity_type)->load($params[$entity_type]);

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.