I've created some PHP code to get the node type, use it to get a filename, check if there is a banner image existing by that name; if the file doesn't exist, I render the default image.

In the images folder I'd have the following images.

  • images/content_type.jpg
  • images/default.jpg
  • images/careers.jpg

If the content type is careers, and there is an image called careers.jpg, then that image is displayed, but if that image isn't found then default.jpg is displayed.

    $type = $node->type;  // This is line 131.
    $type = preg_replace('/\s+/', '-', $type);
    $type = strtolower($type);

    $base_path_trimmed = $base_path;
    $base_path_trimmed = rtrim($base_path_trimmed, '/');

    $img_url = "$base_path_trimmed/sites/default/files/images/banners/sub-banners/$type.jpg"; //placed in variable so it's rendered out in $image
    $image = $_SERVER['DOCUMENT_ROOT'] . $img_url; //php is weird and checks from the server root rather than web root. 

    if (file_exists($image)) {
      print "<img src=\"$img_url\"/ >\n";
    else {
      print "<img src=\"$base_path_trimmed/sites/default/files/images/banners/sub-banners/sub-page-banner.jpg\"/>\n";

The code works, except I sometimes get the following errors:

Notice: Undefined variable: node in include() (line 131 of templates/page.tpl.php).
Notice: Trying to get property of non-object in include() (line 131 of templates/page.tpl.php).

What can I do to avoid those errors? Is there a better way to write the code I need?

EDIT: Better way is to use the Dynamic Banner Module.


If your site is barfing on

$type = $node->type;

then it more than likely means that $node isn't a real node. If you are using this in page.tpl.php, then I would guess it is because the code is being called on a non-node page.

BTW, you shouldn't need to place any code in template files. That is what the preprocess functions are for. If you need to do things with the page template, then check out template_preprocess_page.

  • yeah it did cross my mind that maybe I was using the wrong command in the wrong place. could you give me an example of what i'm trying to do with template_preprocess_page? – Nathan Wright Jun 19 '12 at 0:51

As for improvements that could be made, here's a suggestion:

function THEMENAME_preprocess_page(&$vars) {
    // Sub Page Banner Function
    // Gets title ID (name of page set in drupal), strips spaces and sets to lowercase
    // Checks if img by that name exists in sub page banner folder
    // If it exists render img code
    // If it doesn't render the default banner

    // using content type

    if(isset($vars['node'])) {
        $node = $vars['node'];
    elseif(arg(0) == 'node' && is_numeric(arg(1)) && arg(2) !== 'edit') {
        $node = node_load(arg(1));

    if($node && isset($node->type)) {
        $type = $node->type;

        // file_build_uri returns drupal path:
        // public://images/banners/sub-banners/$type.jpg
        $img_url = file_build_uri("images/banners/sub-banners/$type.jpg");
        $default_img_url = file_build_uri('images/banners/sub-banners/sub-page-banner.jpg');

        // file_create_url returns the real path to the image
        $img_url = file_create_url($img_url);
        $default_img_url = file_create_url($default_img_url);

        // Build associative arrays to pass to theme('image');
        $img_vars = array(
          'path' => $img_url,
          'title' => $type,
          'alt' => $type,
        $default_img_vars = array(
          'path' => $default_img_url,
          'title' => $type,
          'alt' => $type,

        if(file_exists($img_url)) {
            $image = theme('image', $img_vars);
        else {
          $image = theme('image', $default_img_vars);

        // This will be available as $banner_image
        // in your page.tpl.php
        $vars['banner_image'] = $image;

Place the above code in template.php and replace THEMENAME with the name of your theme (Don't forget to check if same function already exists).

Then $banner_image variable will be available in your page.tpl.php.

Don't forget to clear the cache!

  • Okay, thankyou for that seems to be quite effective. after a bit of stuffing round I've gotten it working. Had to merge it with an existing preprocess_page function and add $image = $img_url earlier in the code and now it works! Thanks so much! – Nathan Wright Jun 20 '12 at 0:52
  • if(file_exists($image)) { should be if(file_exists($img_url)) { I've corrected the mistake above. – Beebee Jun 20 '12 at 8:12
  • I'm still getting some errors. on the search page, site map pages etc. Notice: Undefined variable: node in wdhs_preprocess_page() (line 497 of sites/all/themes/zen/wdhs/template.php). – Nathan Wright Jun 27 '12 at 23:09

$node is not always defined when page.tpl.php is executed. It is only defined when you are actually viewing an node page. But page.tpl.php is used on all pages.

Perhaps you want to wrap your code into an if-block like

    if ($node) {

The more complex but still sound solution is to rewrite it like in the answer from Baysaa.

  • this was helpful thanks, stops errors on admin pages that were still showing up using Baysaa's method. – Nathan Wright Jun 21 '12 at 6:35

Better way is to use the Dynamic Banner Module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.