Trouble with $messages variable in Drupal 7

I wanted to add a conditional statement to my html.tpl.php file that would refer to the variable that is created when error/status messages exist. This I believed to be $messages, based on the statement that prints the messages in page.tpl.php. So naturally, I wrote:

<?php if ($messages): ?>
html goes here
<?php endif; ?>

But my HTML is not printed when a message occurs and I just get a warning about an undefined variable on the first line of my statement. I’m normally quite comfortable with PHP, so it really catches me out when a simple conditional statement throws an error.

I really could use a complete list of Drupal variables, but just knowing what I actually should reference in this case would be great for now.

(Have Googled: no luck.)

Are you sure you want to be in html.tpl.php? I would think that if you want to print something to the screen you would do it in page.tpl.php, I’ve only used html.tpl.php in one D7 site as far as I can recall.

Now as far as doing something with this unless it’s really trivial I would probably overload the function in a custom template.php file so that it generates my message in a variable and then just place it in page.tpl.php but I’d need to know more about what you’re doing to figure that out.

Google probably won’t help much because the terms you’d have to use to get results may be too generic like “Drupal Error function”. I would use the Drupal website. Check out the API:

What you’re doing is theming so I would concentrate on that area:

Good luck!


Hi Andrew. I don’t think that it would matter which template file it’s in. The mark-up to which I’m adding extra elements is in the top-level template by default. I could move it if it has an advantage, but it didn’t seem necessary.

Re your 2nd paragraph: I’m not at all sure what you mean there.

From the HTML template:

<body class="<?php print $classes; ?>" <?php print $attributes;?>>
<ul id="skip-links">
<!-- my addition starts -->
<?php if ($messages): ?>
<li><a href="#messages" class="element-invisible element-focusable">Skip to error messages</a></li>
<?php endif; ?>
<!-- my addition ends -->
<li><a href="#content" class="element-invisible element-focusable">Skip to main content</a></li>
<li><a href="#search-block-form" class="element-invisible element-focusable">Skip to search</a></li>
<?php print $page_top; ?>
<?php print $page; ?>
<?php print $page_bottom; ?>

And from the Page template, with a small modification (an expedient fix unless/until I know how to over-write HTML that is not included in templates):

<?php if ($messages): ?>
<div id="messages"><?php print $messages; ?></div>
<?php endif; ?>

Yeah, you’re definitely in the wrong template. D7 is different from its predecessors because anything that has to do with the document type (html4, xhtml, html5) or code in the head of the doc would be done in html.tpl.php. As I mentioned earlier I’ve only modded html.tpl.php on one site and it was definitely an edge case for a weird set of circumstances. Drupal has hooks that let you modify a great deal of what goes in without messing with html.tpl.php.

You’re adding something between <body> and </body> which means that you need to add it to page.tpl.php.

Paragraph 2 refers to over-writing the HTML that Drupal creates. Drupal’s API is responsible for everything that happens within the framework and that includes what is printed to the page. If you want to override what’s being done you either write a custom module and install it or if it’s fairly simple you write a function that will overload the original function and place that function within a file called template.php in your theme folder. It’s a fairly complicated subject to discuss in broad terms but that is where you want to look at to overwrite what Drupal does.

Here’s some more reading about modding Drupal:

Well, OK, but I don’t understand why it’s OK for Drupal’s default html.tpl.php to contain HTML between the body tags, but not OK for someone to change that HTML without moving it into another template.

I’ve read that page to which you linked, and its related pages, a few times recently and either the docs are very poorly written and incomplete or I’m more dense than I thought!

Thanks, though, Andrew.

Well IMO, they really improved on the system/theme part of Drupal with D7 by making it more modular and separating the document template (html.tpl) from the content template (page.tpl). Prior to D7 you used to have page.tpl.php that did everything. Now that they have a clear separation, it’s very simple to change from a xhtml doc and do an html5 doc or better yet some mobile device friendly doc.

As far as content within the html.tpl file, it doesn’t seem to be content html that is included, just reference to variable blocks and a “skip to main content link”. I’ve never used the skip link so I’m not sure why it’s there but perhaps because us developers might take it out if we had the chance :wink:

I haven’t looked or tried to access page variables from within the html template but it could be that the variables you want to access from within html.tpl.php are only available from within page.tpl.php.

Also, don’t worry about being dense when it comes to figuring this stuff out. If you keep at it, it will start to make sense. Also since D7 has changed the game so much over D6, the documentation is still being worked on. There is a “Drupal way” of doing things and it takes a while to get the philosophy of how things are done. I would start with a simple vanilla Drupal 7 site and maybe just look at the default Bartik theme and look at the template.php file. Within that file you’ll see bunch of preprocessor functions that run when a page is generated. Some of them add classes to navigational links or change the way the breadcrumbs work or add classes to the body tag. Reviewing that will give you an idea of how things are done. It might seem daunting at first but also know that when you create a custom theme you can have as many or as few of these functions as you need. I’m in the midst of developing a site right now for a graphic design and it doesn’t even have a template.php file… I don’t need one.

  • If you need to do some server side processing to create a variable that you’ll want to grab on the page, template.php is a good place to do it within the THEME-NAME_preprocess_html() function or the THEME-NAME_preprocess_page() function. This is an old post but it describes the idea behind it:

Keep plugging away and it will be rewarding. Drupal is like a box of lego bricks. You can make it do anything but you have to do the engineering and put the pieces together to do so.