Hi there,

I'm currently trying to write a module which will present content in a block based on the current node. Essentially I'd like to be able to inspect the node's fields and present some of them in a block.

How can I get to the node from within the module? I wasn't surprised to see that $node and $nid are null when I tried to reference them from hook_block.

Any suggestions would be greatly appreicated.

Comments

RichieRich’s picture

Calling

$node = menu_get_object();

Seems to give me a valid node reference. Great!

anil614sagar’s picture

You have to do $node = node_load(arg(1)) to get the node object of the current page inside your hook_block.

The below links will help you to solve problem.

http://api.drupal.org/api/function/node_load/6
http://api.drupal.org/api/function/hook_block

Cheers,
R. ANIL SAGAR,
Sr. Drupal Developer,
SourceN,
Bangalore.
http://www.sourcen.com
http://www.anilsagar.com

Cheers,
Anil Sagar,
Lead Drupal Developer,
Azri Soulutions,
http://azrisolutions.com/

RichieRich’s picture

The solution which I originally posted worked fine in terms of getting the current node object.

However, the node references other nodes. I iterate through these and load associated fields via node_load in order to create a 'related nodes' block.

Wolfflow’s picture

FYI: Views

Contact me for drupal projects in English, German, Italian, Drupal Hosting Support.

RichieRich’s picture

I tried the views approach but I simply couldn't get it to work properly (I was using arguments). I had two key problems:

1) The block was displayed even on pages where there were no results.

2) When applying a filter in order to ensure that the block was only displayed on the correct pages I ended up with repeated results. Each of my pages where the block should appear has three related pages - I was ending up with each group of three being presented 3 times. Using the option to reduce duplicates within views had no effect whatsoever.

I'm glad I've moved to doing it with a module - it's been the first module which I've written and it's given me more flexiblity than I would have had even if the views approach had worked.

chakrapani’s picture

You can add the following condition at the beginning of block content to avoid block display in non-node type pages
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
//you can further add any conditions based on the node object here.
}

Regards
Chakrapani

RichieRich’s picture

Thanks, but there's stuff that I simply couldn't do with views. I'm loading nodes via a node reference and then displaying a teaser picture which is cck member of the referenced node. I doubt that I could do this with views, but I could be wrong. Please let me know if that's the case.

RichieRich’s picture

Just in case anybody is interested, my module was used to create the 'Related Pages' block in the right-hand column (may be the left side by the time you read this).

Image is inserted referenced via the node reference and embedded in a link to the page. Preview text will also be added soon (that too will be loaded from referenced nodes).

http://www.ishigaki-japan.com/sights/kabira

bmango’s picture

menu_get_object() doesn't work when you have clean URLs enabled. To get it to work you will first need to use drupal_get_path() to convert the aliased URL to drupal's internal path.

I spent half a day finding this out!

Ben

Drupal Website Design