3

I have a URL like track-new/15/51. arg(2) returns 51.

How can I check if the content type of the node whose ID is 51 (the value returned from arg(2)) is 'artist'?

4

If the value returned from arg(2) is a node ID, you can load the node with node_load(). This will return a node object, which contains its content type.

$node = node_load(arg(2));
$content_type = $node->type;
  • drush php-eval '$node = node_load(51); return $node->type;' – leymannx Nov 20 '19 at 11:55
2

Although I am a big fan of node_load(), it is possible to get the type of a node from a node nid in a more resource friendly way. Here's a helper function to get a node's type given a nid:

/**
 * Given a node id, this returns the node's type or NULL if it doesn't exist.
 * @param $nid
 * @return {String|NULL}
 */
function example_get_node_type($nid) {
  $query = db_select('node', 'n')
    ->fields('n', array('type'))
    ->condition('n.nid', $nid)
    ->execute();
  $result = $query->fetchObject();
  return $result->type;
}

Then you can use it with something like:

if (example_get_node_type(arg(2)) == 'artist') {
  // Do something...
}

Or if you already have the node id, something like this:

$nid = 123;
if (example_get_node_type($nid) == 'artist') {
  // Do something...
}
1

A bit of validation before calling node_load is never harmful:

if (arg(2) && is_numeric(arg(2)) {
    $node = node_load((int) $a2);
    $type = $node->type;
}
  • 2
    Fixed a couple small issues in the code: 1. The isset() was being done on the entire condition rather than just arg(2), and 2. you can't do an isset() on the return of a function (ie. return value in write context). – Yuriy Babenko Oct 29 '13 at 18:21
1

Use menu_get_object() to retrieve data loaded by the current menu-router item. In your case you can get to the node like this:

$node = menu_get_object('node', 2);
$node->type;
0

You can use something like this:

$menu_item = menu_get_item();

    // Test if this is a term page.
    if (!strncmp($menu_item['path'], 'taxonomy/term/%', 15)) {
        $obj = menu_get_object('taxonomy_term', 2);
  • The OP is asking how to get a node from its node ID that is passed in the URL. It's not asking about taxonomy terms. – kiamlaluno Nov 8 '19 at 11:03
0

If the route for that path is, for example, track-new/%/%node or track-new/%user/%node, I would use the following code.

if ($node = menu_get_object('node', 2)) {
  $content_type = $node->type;
}

If the route for that path is, for example, track-new/%/% or track-new/%user/%, I would use the following code.

$nid = arg(2);
if (!empty($nid) && is_numeric($nid) && ($node = node_load($nid))) {
  $content_type = $node->type;
}

menu_get_object('node', 2) doesn't work with a route like track-new/%/% because it expects the load function for the last part of the path to be node_load(), which isn't true for a route like track-new/%/%.
In fact, the code used from menu_get_object() is the following one.

  $router_item = menu_get_item($path);
  if (isset($router_item['load_functions'][$position]) && !empty($router_item['map'][$position]) && $router_item['load_functions'][$position] == $type . '_load') {
    return $router_item['map'][$position];
  }

I would always use node_load() when I want to load the node object associated with the current path independently from how the route has been defined.

I am using node_load() instead of querying directly the database because:

  • It's possible that I would need other node properties, even if that could happen in future versions of the module I am writing
  • The nodes loaded with node_load() are cached in memory, which means that the code I am using could not even cause any database query

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.