So far, I came across these 3 functions, 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?
|
So far, I came across these 3 functions, 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? |
|||||
|
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 you need to get the node object, then you can use the following code:
Between the following two snippets, the latter is better, as it returns the correct node object for internal paths such as node/1/revisions/3/view.
|
||||
|
|
|
One thing you have to be careful of is path alias.
to get the current path use the system variable (If you are on the node page you can call this will return node/XXXX so try something like
|
||||
|
|
Is not a bad way of doing it. arg() works on the internal path, so it won't be affected by path aliases. |
|||||||
|
|
How it's done in Drupal Core's template_preprocess_page: Line 2267 of theme.inc and onwards:
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. |
|||
|
|