Skip to content

Instantly share code, notes, and snippets.

@mohammadanwar
Forked from thagler/get_node_by_path.php
Created August 28, 2024 06:46
Show Gist options
  • Select an option

  • Save mohammadanwar/59a4d5b485f2bb64c9e5a0070203893e to your computer and use it in GitHub Desktop.

Select an option

Save mohammadanwar/59a4d5b485f2bb64c9e5a0070203893e to your computer and use it in GitHub Desktop.
Drupal 8 Get node ID by path alias
<?php
$path = \Drupal::service('path.alias_manager')->getPathByAlias('/this-is-the-alias');
if(preg_match('/node\/(\d+)/', $path, $matches)) {
$node = \Drupal\node\Entity\Node::load($matches[1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment