Skip to main content

Quick tips: Removing $title and $breadcrumbs in Drupal 7

An article from ComputerMinds - Building with Drupal in the UK since 2005
2nd Feb 2011

Stephen Tweeddale

Senior Developer
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

In Drupal 6 you could get hold of and manipulate all the variables available to a given template file in a preprocess function, within your themes template.php, such as template_preprocess_page() for the page.tpl.php. This was a handy way of, say, conditionally removing the page title on pages of a certain node type: find the title in the $variables array passed to your preprocess function and unset() it (If this doesn't make any sense, I recommend this video tutorial from Mustardseed Media about manipulating variables within template.php).

However, if you try this trick in Drupal 7 with either the page title or breadcrumbs, you may find yourself pulling hairs seaching the $variables array for them. That's because in Drupal 7 there's template_process_page() which, as you might guess from the name, runs after your preprocess function. In that function, drupal_get_title() and drupal_get_breadcrumb() get called, with the results being added to the list of variables available to page.tpl.php.

So how can these things be overridden? If you look at that function you will see that they're added conditionally using isset(). So we can set the title in our preprocess and it will override whatever would be otherwise set by drupal_get_title().

Unfortunately though, we can't then use unset() to totally remove the variable as in Drupal 6, but we can (somewhat un-intuitively) set them to an empty string and rely on the lazy typing of php in our page.tpl.php.

To explain, if you put this in your template.php:


function mytheme_preprocess_page(&$vars) {
  $vars['breadcrumb'] = '';
}

And then used logic such as the following in your page.tpl.php, we'd get an empty h1 tag:


if (isset($title)) {
  print "

{$title}

"; }

...because that variable is set, just to an empty string. But, because an empty string directly evaluated (type-cast to a true/false boolean) equates to false, we wouldn't if we did the following:


if ($title) {
  print "

{$title}

"; }

Most themes use this second method of evaluation to check if a variable should be printed or not. This is fine, because variables passed to page.tpl.php are almost always strings of HTML. It's worth knowing though that if the variable your testing is the string '0', for example, PHP would cast that to the boolean value false, and not get printed.

It should also be noted that if you find yourself writing modules though, you should avoid this kind of if ($variable) condition: an empty array equates to false, while an empty object equates to true...and so begins the journey down the fascinating and murky rabbit hole of PHP lazy-typing...in fact don't be surprised if there's a follow up article discussing some of the common pitfalls in Drupal.

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.