I'm in the process of upgrading from 5.x to 6.x (on the way to 7.x), and I decided to move a bunch of code which was working fine in my 5.x template.tpl into a module to make future upgrades easier.
I found some advice on the drupal site about using mymodule_preprocess_node to add variables, and used that successfully to add a variable, generated from information in $node, to my blog-nodes for theming when build-mode was full.
I wanted to add another, for when build mode was teaser, but suddenly find that my module doesn't have access to the $node object when in teaser mode.
Here's the code, minus the first variable which is rather complex:
function dm_variables_preprocess_node(&$vars) {
// populate $var_to_fill
$vars['dm_parent_link'] = dm_parent_link();
}
function dm_parent_link() {
$parentnid_sql = 'SELECT link_path, link_title FROM menu_links WHERE mlid = %d';
$parentnid = $node->book['plid'];
$parent_array = db_fetch_array(db_query($parentnid_sql, $parentnid));
$link_title = $parent_array['link_title'];
$link_path = $parent_array['link_path'];
if ($link_title !== "Etc.") {
$parent_link = "<a href='http://dailymull.com/$link_path' title='View all posts in this topic.' >$link_title</a>";
}
else {
$parent_link = "";
}
return $parent_link;
} // function dm_parent_link()
I can get this to do the job, if I assign a value for $parentnid directly, but every test I run to check on $node comes up negative--even though the other variable, which depends on values from $node, runs perfectly, so the question seems to be why the module can only access a full node and not one in teaser mode, even though the node template which uses the variables accesses both modes.
I've been stuck on this for two weeks now, so any help will be tremendously appreciated.
Comments
The is no global $node,
The is no global $node, change dm_parent_link() so it takes $node as an argument and change the call to
dm_parent_link($vars['node']);Thanks
That did it. Thanks so much.
I'm left wondering why the other variable worked, when the node was in full mode...
But now I know how to do it right, I'll change the code there as well.
Thanks again.