So far, I came across these 3 functions in Drupal 7, but I am not really sure what the difference between them is, and which is more appropriated to get the node ID in a block preprocess function.
Can you give me any insight?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communitySo far, I came across these 3 functions in Drupal 7, but I am not really sure what the difference between them is, and which is more appropriated to get the node ID in a block preprocess function.
Can you give me any insight?
I am not really sure what the difference between them is
menu_get_object()
will return the node object for the node whose ID is 1; if the page being shown is example.com/user/1, then menu_get_object('user')
will return the user object for the user whose ID is 1. node_load
) to be called to load an object corresponding to a part of the current pathdrupal_lookup_path("homepage")
will return "node/1"
. Path aliases can be associated to any internal paths, not only to node paths.and which is more appropriated to get the node ID in a block preprocess function.
If all you want to get is the node ID of the node currently shown, then you just need to use the following code:
if (arg(0) == 'node') {
$nid = arg(1);
}
If you need to get the node object, then you can use the following code:
if ($node = menu_get_object()) {
// Use the node object.
}
Of those snippets, the second is better, as it returns the correct node object for internal paths such as node/1/revisions/3/view.
Keep in mind that some preprocess functions, such as the ones for the page, or the node, have already a $variables['node']
. This means that in those cases, you just need to use the following snippet in your preprocess function, without worrying about which function to use.
if (isset($variables['node'])) {
// Check the node ID or other properties.
}
For other preprocess functions, check the documentation to see if there is any variable containing the node object. For example, that is the case for template_preprocess_comment()
which makes $variables['node']
available to the preprocess functions for comments.
How it's done in Drupal Core's template_preprocess_page:
Line 2267 of theme.inc and onwards:
if ($node = menu_get_object()) {
$variables['node'] = $node;
}
template_preprocess_page uses menu_get_object. After that it's just a matter of checking if the node is defined and getting $node->nid.
In Drupal 8, you can get url arguments based on what's in the routing.
To get the id alone:
$nid = \Drupal::routeMatch()->getRawParameter('node');
If you plan to load the node object, you can simply get it this way:
$node = \Drupal::routeMatch()->getParameter('node');
To find the parameter names, you can use:
$possible_parameters = \Drupal::routeMatch()->getParameters();
One thing you have to be careful of is path alias.
drupal_get_normal_path($path)
to get the current path use the system variable $_SERVER["REQUEST_URI"]
(If you are on the node page you can call $node->uri['path']
, this will return a non-alias path. Then again if you are on the node page, you can just use $node->nid
.)
this will return node/XXXX
so try something like
$node_path = explode('/', drupal_get_normal_path($path));
$nid = $node_path[1];
arg(1);
Is not a bad way of doing it. arg() works on the internal path, so it won't be affected by path aliases.
menu_get_object
(which just wraps around menu_get_item
so that's equally good).
if (arg(0) == 'node' && is_numeric(arg(1)))
then arg(1)
will to nearly 99% return you a node ID. But a solely arg(1)
could also return you a lot of other IDs.