Hi,
I want to set custom block in my module to show only on specific node type. The only solution I know to get the node->type into hook_block is as below:

$nid = arg(1);
$node = node_load($nid);
$type = $node->type;

Is it ok to use the above code in hook_block? or is there a better solution to get a node->type inside the block hook?

I am bit worried about loading the node using node_load as it might be memory intensive.

Thanks for your time.

neokrish.

Comments

swentel’s picture

You shouldn't worry using node_load(). If you're on a node page, the node object will be statically cached, so nothing extra happens there. You can find more snippets on http://drupal.org/node/64135 - note you can either use arg(1) or menu_get_object().

intyms’s picture

Hello.
I would like to check the "node_type" of a list of nodes.
So, using node_load isn't the best solution for me because it will load into the memory all nodes.

can anybody help to get the "node type" by "nid"?
Thanks!

yuriy.babenko’s picture

The simplest thing to do is run a query:

$nids = array(1, 2, 3); //array of your node ids

$result = db_query("SELECT nid, type FROM {node} WHERE nid IN (%s)", implode(',', $nids));

while($row = db_fetch_object($result)) {
    echo $row->nid.' : '.$row->type.'<br />';
}

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

nerdydragon’s picture

Check out get_defined_vars() Here's a good write-up: http://www.dottedidesign.com/node/36