How can I get the node ID from a path alias?

By toswebdev, 14 April, 2023
How can I get the node ID from a path alias?
  $path = \Drupal::service('path_alias.manager')->getPathByAlias($article_url);
   if (preg_match('/node\/(\d+)/', $path, $matches)) {
     $node = \Drupal::entityTypeManager()->getStorage('node')->load($matches[1]);
   }

Comments

All comments go through moderation, so your comment won't display immediately.