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'
?
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 communityI 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'
?
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;
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...
}
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;
}
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
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;
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);
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:
node_load()
are cached in memory, which means that the code I am using could not even cause any database query