tags:

views:

24

answers:

1

I'm working in Drupal 6.

I have a requirement to add a particular block when the user is on a blog page. Sounds simple enough, but it's been driving me mad.

The block needs to be shown when the user is viewing a blog overview or an individual blog entry.

I initially thought I could filter it by page name so it only appears when page = /blog/. Unfortunately, that only applies to the blog overview page; the individual blog entry pages have their own URLs (default is /node/ but will be changed to whatever the owner wants).

A bit more googling, and I found out about $node->type=='blog' which should pick up the fact that I'm on a blog entry pages, but doesn't seem to work.

In the admin/build/block/configure page I have page visibility set to PHP mode, and PHP code as follows:

<?php
return ($node->type == 'blog');
?>

but that doesn't seem to work, even though if I print_r($node) in the template, it does show type==blog.

I also added strpos($_SERVER['REQUEST_URI','blog') to the above, but of course since the first condition isn't working, adding the second won't help.

It feels like there should be an obvious answer, but I just can't find it. Can anyone help me out. Thanks.

+1  A: 

Your problem with the above code, is that when you run the code for the block, it wont have the $node variable available. You need to do something like this to add it to blog nodes.

<?php
    // This code checks the internal url, which for nodes always will be node/[nid].
    // Last condition: don't display the block on node edit forms etc.
    if (arg(0) == 'node' && is_numeric(arg(1)) && empty(arg(2))) {
      $node = node_load(arg(1));
      return $node->type == 'blog';
    }
?>
googletorp
The code is ugly and non-obvious, but does appear to work (after syntax correction! ;) ), so thank you for that.Also thanks to Nikit who suggested much the same thing. Given the similarity in the two comments, I presume it's documented somewhere on the drupal site? I guess my google-fu isn't as strong as I would like to think.Is there any major overhead to calling node_load() all the time like this?
Spudley
node_load calls are cached (static) so there is no overhead, as the node you are viewing will be loaded anyway.
Tom