By silkAdmin

2011-07-08 12:15:14 8 Comments

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?


@kiamlaluno 2011-07-08 19:44:11

I am not really sure what the difference between them is

  • menu_get_object() returns the object associated with the page currently shown. If the code is executed when Drupal is showing the page, then menu_get_object() will return the node object for the node whose ID is 1; if the page being shown is, then menu_get_object('user') will return the user object for the user whose ID is 1.
  • menu_get_item() returns an array containing an index for each field contained in the "menu_router" table. This includes:
    • path: the current path of the menu callback
    • load_functions: an array of function names (like node_load) to be called to load an object corresponding to a part of the current path
    • access_callback: the callback that checks if the currently logged in user has access to the menu item
    • page_callback: the name of the function that renders the page
    • title: the title for the page associated to the menu item
  • drupal_lookup_path() is the function that returns you the internal path associated with the alias passed as argument. If "homepage" is the alias associated to, then drupal_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.

@oknate 2016-05-25 19:37:35

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();

@iStryker 2011-07-08 12:34:52

One thing you have to be careful of is path alias.


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];

@Bart 2011-07-08 15:09:48

How it's done in Drupal Core's template_preprocess_page:

Line 2267 of 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.

@Jeremy French 2011-07-08 13:17:38


Is not a bad way of doing it. arg() works on the internal path, so it won't be affected by path aliases.

@iStryker 2011-07-08 13:29:36

This is true, it won't be affected by path aliases. But if you are on node/XXXXX, then you can just use $node->nid. @French, where else would you use arg(1) and it would return what you want?

@Jeremy French 2011-07-08 13:51:49

if you are in a module hook you won't have access to node. Views arguments may be an example as well.

@silkAdmin 2011-07-08 14:16:50

thanks jeremy and istryker, i have updated my question it might be clearer.. Though i ll try both of your solutions, thanks

@user49 2014-04-01 22:18:57

It is a very bad idea to use arg() , you are binding yourself to a path, use menu_get_object (which just wraps around menu_get_item so that's equally good).

@leymannx 2017-05-06 23:17:18

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.

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] Insert row into a view

  • 2011-08-31 14:14:41
  • Philipp Horn
  • 6959 View
  • 6 Score
  • 3 Answer
  • Tags:   7 views

2 Answered Questions

[SOLVED] commerce_line_item VS field_data_commerce_line_items

2 Answered Questions

[SOLVED] When should I use behaviors? Can I attach them to any DOM node?

  • 2016-09-30 16:08:59
  • NicholasByDesign
  • 608 View
  • 0 Score
  • 2 Answer
  • Tags:   7 javascript

1 Answered Questions

[SOLVED] Unable to set URL alias

  • 2014-06-08 04:39:39
  • open_sourse
  • 65 View
  • 0 Score
  • 1 Answer
  • Tags:   uri

1 Answered Questions

[SOLVED] How to hide language switcher block if node has no translation?

  • 2014-01-15 11:43:23
  • leymannx
  • 1494 View
  • 1 Score
  • 1 Answer
  • Tags:   7 i18n-l10n blocks

2 Answered Questions

1 Answered Questions

Make variables outside of node available to pathauto

2 Answered Questions

[SOLVED] Pass variables from html preprocess function to page preprocess function?

  • 2012-03-04 13:52:56
  • maxedison
  • 6766 View
  • 10 Score
  • 2 Answer
  • Tags:   7 theming

Sponsored Content