As I move into Drupal 7, I'm suddenly very confused about something that I thought I understood (or didn't need to understand in the considerable amount of time I've spent in Drupals 5 and 6 --

I'll be working on a module and, in the process of developing the code, I'll throw in a few calls to dsm() and/or drupal_set_message(), perhaps in node--something.tpl.php or in a function that gets called from the template file. In my previous work, these calls always worked the way I expected -- on the next page submission, I'd get the results of the dsm's. In Drupal 7, I'm doing the same thing, but am finding that they're showing up one page too late -- I have to refresh the page to see them. In trying to figure this out, I began to think that the Drupal 7 page rendering model is that the selected page template gets evaluated and then -- and only then -- does the node being displayed on the page get loaded and rendered. Sure enough, a few tests with watchdog() and sleep() confirm this -- on one of my Drupal 6 sites, the node template file is run before the page template file, but, on my Drupal 7 site, the page template file is run before the node template file. This is with only-very-slightly changed versions of the standard D7 page template, and no funky tweakings of internal D7 behavior, btw, and I'm doing nothing special with caching -- all of the caching options on Config > Development > Performance are turned off.

This explains the message behavior I'm seeing in D7, but it really seems like the wrong thing -- I want those messages to appear as part of the immediate rendering of the page like they do on my D6 sites, not on a refresh of the page. This isn't just a matter of debugging convenience; it's also messing up some form validation code where a form is created by the rendering of a node, and validation error messages don't appear on the page produced by the form submission.

Or so I think, anyway. Does anyone know what's going on here? Was there a change in D7 that led to this seemingly new behavior, or have I somehow screwed something up on my site? I'd be happy to throw some debugging code into my sites if someone can point me in the right direction. Many thanks for whatever clarifications might be out there....

Comments

marcingy’s picture

Category: bug » support
jim_at_miramontes’s picture

Hello? Your message didn't seem to come through....

1kenthomas’s picture

Hmm. I'm having dsm() in node templates, not work at all. Similar issue?!?

damien tournoud’s picture

Status: Active » Closed (works as designed)

The rendering of the node template and the rendering of the node content are two different things. Yes, the structure of the node might be generated either before or after the rendering of the system messages, but the rendering of the content of the node is always done before that.

andy tawse’s picture

with regards to "also messing up some form validation code" - I had the same problem and the way I got round this was to embed the form in a block. Doing a drupal_get_form() in the template or preprocessor is too late in Drupal 7.

Chim’s picture

We solved the form validation problem, (form validation happening after the messages have been rendered, and showing up on the next request), by..

forcing the form validation in hook_node_view

/**
 * This is a fix for a bug where the validation error messages lag a 
 * full request behind form submissions
 */
function your_module_node_view($node, $view_mode){
  // check that form id is the form you want
  if ( !isset( $_POST['form_id'] ) || $_POST['form_id'] != 'your_form_id' ){
    return;
  }
  // this forces validation of the form 
  drupal_get_form('your_form_id');
}

tonebari’s picture

That wasn't my solution, but you pointed me in the right direction... BTW, folks when you post a bogus answer man or woman up and get back and admit it...

<?php
if($_POST['form_id'] && $_POST['form_id'] == 'kns_user_location_form'){
	drupal_get_form('kns_user_location_form');
}
?>