Michał Pękała
Michał Pękała

Reputation: 2389

Drupal: how to pass variable from node to block?

I'd like to do in

  1. do some processing of node fields in node.tpl.php
  2. save it to a variable
  3. and display in a block that is loaded after the node (on the same page).

How to pass a variable from a node to a block?

Upvotes: 1

Views: 3660

Answers (2)

Jeremy French
Jeremy French

Reputation: 12157

do some processing of node fields in node.tpl.php

A purist would say that you shouldn't be doing it there. hook_node_load() can be used for processing of fields.

Then you can just use menu_get_item() in your block to get the $node object and access any values you have added to it.

Upvotes: 1

jhedstrom
jhedstrom

Reputation: 3388

Since you're running Drupal 7, you have access to the new hook_page_alter() function, from either a custom module or theme. By implementing this, you can easily move parts of the main content area (eg, the node), into any number of different blocks. You'll want to use the show(), hide() and render() functions to properly hide content.

Alternatively, using show() and hide() from within your node.tpl.php template should properly hide the elements, and then you could grab the node object from your block using the menu_get_object() function.

There is normally a video available on hook_page_alter() here, but the site is down at the moment.

Upvotes: 3

Related Questions