Skip to content

Instantly share code, notes, and snippets.

@manojiksula
Forked from crittermike/get_node_by_path.php
Created August 11, 2020 13:45
Show Gist options
  • Save manojiksula/9bb1d0bd5c7b702c344f21f98fa77b21 to your computer and use it in GitHub Desktop.
Save manojiksula/9bb1d0bd5c7b702c344f21f98fa77b21 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]);
}
@levmyshkin
Copy link

levmyshkin commented Jul 24, 2023

Drupal 9+ Get node ID by path alias:

<?php

$path = \Drupal::service('path_alias.manager')->getPathByAlias($current_path);
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