I have a application that requires me to pass a variable into a block.
Specifically, I want to pass the latitude and longitude of the active node into a block on the page. I then use this information to access a database to locate nearby nodes and the relative distance from the active node. I have been able to use $_SESSION variable to pass the information BUT the block is always lagging behind the page.
For example, the block SESSION variable does not update to the correct value until the first refresh of the page. This is because the SESSION variable is set in the template.php file and obviously this does not get run before the block is created.
I either need to place the SESSION variable in sequence before the creation of the block and after the selection of the node or I need a different way to pass the variable into the block.
Has anyone else found a solution for this type of problem?
Comments
Have you tried...
Either using a preprocess templating function(Drupal 6) or _phptemplate_variables (Drupal 5)? You should be able to query from the path what node you're on, and add the appropriate coordinates to a variable accordingly.
More info:
http://drupal.org/node/16383 (Drupal 5 - How to use _phptemplate_variables function)
http://drupal.org/node/223430 (Drupal 6 - Preprocess functions for themes)
If you need additional assistance/pointers, you can contact me from my Drupal page.
This is what I am currently
This is what I am currently doing but I still have the problem, maybe I did it wrong - let me explain.
In the node case the variable is : $vars['node']->location['latitude'] and I set the SESSION variable at this time:
case 'node':
$_SESSION['latitude'] = $vars['node']->location['latitude'];
$_SESSION['longitude'] = $vars['node']->location['longitude'];
break;
In the block case this variable ($vars['node']->location['latitude']) does not exist and therefore if I set the SESSION variable it is just blank.
I then use the SESSION variable in my block, but it requires a refresh of the page in order to have the SESSION variable in the block to update.
Am I using the _phptemplate_variables correctly? BTW I am using Drupal 5.
I see what I did wrong but I
I see what I did wrong but I still cannot get it to work with Panels.
I can now pass the variable into blocks using the technique you recommended. It does not work with block that I present in a panel. For example if I put the block in a pane of a panel page the variable do not exist. What is up with that?
Okay, This seems to be an
Okay, This seems to be an issue with using the block in a Panel. It works perfectly when I do not use panels.
I was going to reply based
I was going to reply based on your question of active node which I take to mean you are viewing a page at node/{nid} but the panels question threw me for a loop. Are you overriding node/{nid} to use panels? If not how do you define the active node?
Yes, I am using Panel to
Yes, I am using Panel to create a node override for this content type.
If that is a CCK field you
If that is a CCK field you may want to investigate the CCK block module.
In any case you can use the following code to get the node in side the block, this will work when the block is on a page of the form node/{nid} (unaliased path), it will also work if the path has an alias.
Thanks nevets
That is just what I was looking for. However, isnumeric should be is_numeric =)