1

What is reliable way to get the node id from the path alias in drupal 8?

3

You can try this code:

$path = \Drupal::service('path.alias_manager')
      ->getPathByAlias('alias/path');

    if (preg_match('/node\/(\d+)/', $path, $matches)) {
      $nid = $matches[1];
    }
    else {
      $nid = '';
    }
  • What about if I want to get the node id not in coding... but in backend. – Vishal Kumar Sahu Aug 2 '17 at 12:02
  • This comment does not make sense. The node will always be the first argument on any path node/. – Kevin Aug 2 '17 at 13:31
1

Install the devel module and then use the devel toolbar item Current route info.

This results in this url, which you can type in directly and provide any path known to your drupal installation as query parameter:

/devel/routes/item?path=alias

When it is a node you find the node id in the raw parameters at the end of the listed route variables.

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.