How do I get the current node ID?
Solutions
It is correct to use \Drupal::routeMatch()->getParameter('node')
. If you just need the node ID, you can use \Drupal::routeMatch()->getRawParameter('node')
.
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.
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'));
}
Get the breadcrumbs for a specific node other than the current
I have a list of nodes created by a view. Those nodes tend to have titles that are mostly the same, but since they are in a rather logical hierarchy defined by the menu I'd like to output the breadcrumb to the nodes as well. I couldn't find a core-function that does that. Anyone an idea how to work around/on that?
Get current region name in node template Drupal 7
I need to find out what region the node block is appearing in within the node template. I am using node block module to display nodes as blocks but need to know what region the node blocks are appearing in within the node.tpl. In Drupal 6 there was a template preprocess function where you could set a global var to accomplish this but that function ...
How to programmatically get the NID of the current node
I've pored over this old thread on drupal.org and it just kind of makes my head spin around. Pulling down the path and trying to parse out the NID from within it? There has to be a better way. And solutions like don't work in my custom module (though I'm told they work in templates?). No error or anything, it just instantiates $node with a NULL val...
Get all current tabs (node view)
How to get all node tabs (MENU_LOCAL_TASK type) which available for current node? I mean node/%/edit, node/%/translate, node/%/devel etc. I've tried to use menu_local_tasks() but it shows only empty array.
Get list of taxonomy terms for current node through Views
I've been trying this for awhile, and I keep finding answers that are close - but not quite. If there is a node with taxonomy terms attached to it, I want to create a block in Views that I can place in the sidebar that will load the terms that are attached to THAT node. I want to do it through Views so I can rewrite the URL with a custom query usin...