46

In Drupal 7, if I wanted to get to get the node id of the currently displayed node (e.g. node/145) I could get it with the arg() function. In this case, arg(1) would return 145.

How can I achieve the same in Drupal 8?

88

The parameter will have been upcasted from nid to full node object by the time you get access to it, so:

$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
  // You can get nid and anything else you need from the node object.
  $nid = $node->id();
}

See the change record for more information.

  • Do I access the nid by using a method? Trying to access the nid via $node->nid retunns nothing. – 24ma13wg Jan 29 '15 at 14:51
  • 2
    $node->nid->value? – Clive Jan 29 '15 at 14:52
  • 8
    Strike that: $node->id() is better – Clive Jan 29 '15 at 14:53
  • 3
    I just want to add you have to careful with this - I just got stung where \Drupal::routeMatch()->getParameter('node'); will return an array of 1 item (the node id) on node revision delete pages, so if you calling a method on what you assume to be an object you will get a fatal error like "Fatal error: Call to a member function getType() on string". – Jeff Burnz Aug 10 '16 at 1:13
  • 2
    To be sure you have really a node you can use "if ($node instanceof \Drupal\node\NodeInterface) " condition. (see drupal.org/docs/8/api/entity-api/working-with-the-entity-api) – tdd Nov 28 '16 at 7:48
15

It is correct to use \Drupal::routeMatch()->getParameter('node'). If you just need the node ID, you can use \Drupal::routeMatch()->getRawParameter('node').

4

if you are using or creating custom block then you have to follow this code to get current url node id.

// add libraries
use Drupal\Core\Cache\Cache;  

// code to get nid

$node = \Drupal::routeMatch()->getParameter('node');
  $node->id()  // get current node id (current url node id)


// for cache

public function getCacheTags() {
  //With this when your node change your block will rebuild
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
  //if there is node add its cachetag
    return Cache::mergeTags(parent::getCacheTags(), array('node:' . $node->id()));
  } else {
    //Return default tags instead.
    return parent::getCacheTags();
  }
}

public function getCacheContexts() {
  //if you depends on \Drupal::routeMatch()
  //you must set context of this block with 'route' context tag.
  //Every new route this block will rebuild
  return Cache::mergeContexts(parent::getCacheContexts(), array('route'));
}
  • This post doesn't make sense. There is no way for anybody to know where this code goes. – Lester Peabody Nov 14 '17 at 17:58
1

Note on the node preview page, the following doesn't work:

$node = \Drupal::routeMatch()->getParameter('node');
$nid = $node->id();

For the node preview page, you have to load the node this way:

$node = \Drupal::routeMatch()->getParameter('node_preview');
$nid = $node->id();

How to load node object in node preview page?

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.