The $messages variable for page.tpl.php is generated too early in the process.

The most simple improvement is to move the generation of $messages into template_process_page() (instead of preprocess).

And when doing that, we can additionally apply the current model of process variables for consistency, which adds the benefit of allowing a contributed module to plug in an advanced rendering mechanism for $messages (i.e., a #post_render alike string/token replacement solution).

CommentFileSizeAuthor
#1 drupal.messages.1.patch1.32 KBsun
drupal.messages.0.patch1.57 KBsun
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sun’s picture

FileSize
1.32 KB

Shortened that comment.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Unlike kindergarten, drupal code likes to go last. This change makes sense.

sun’s picture

Priority: Major » Normal
sun’s picture

Title: Messages have to be generated last + allow for advanced ESI solutions » Messages are generated too early
Priority: Normal » Major
Issue tags: +API change

Problem

  • Status/error messages triggered in MODULE_preprocess_page() and THEME_preprocess_page() are not displayed.

Goal

  • Generate and render status/error messages as late as possible.

Details

  • The $messages variable is generated relatively early in template_preprocess_page() already.
  • The generated/rendered variable only contains messages that have been set so far.
  • Any messages that are triggered/set in later preprocess hooks are not output (resp. output on the next page request instead of the current).
  • At least for themes, it may be relatively common to use THEME_preprocess_page() to output a message on certain pages.

Notes

  • This patch only accounts for messages being output in later preprocess hooks. Messages triggered in process hooks or during render($variable) within the template file itself are still not captured.
  • I don't think it makes sense to test this change, as we are still doing the identical thing, just slightly later in time. We need to find a proper solution for this in D8.

API impact

  • Currently, it is technically possible to alter messages in hook_preprocess_page(). Modules or themes that are implementing this hook in order to alter status/error messages need to implement hook_process_page() instead.
Dries’s picture

This make sense to me. That said, I can't think of an actual use case where a preprocess function would generate a status message. That being said, it is easy enough to create such an example.

webchick’s picture

Dries, I'm not sure what to do with this based on your comment in #5. Leaving it to you.

sun’s picture

Some further notes:

- The outlined API change impact, although being true, can be considered moot, because $messages is a HTML string already, so all that a hook_preprocess_page() is able to do with regard to $messages is performing crazy-strange string replacements.

- Messages also means error messages. That's the primary reason for why I think this patch is important.

moshe weitzman’s picture

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks.

rfay’s picture

Based on the API change summary I won't announce this unless otherwise directed.

Status: Fixed » Closed (fixed)
Issue tags: -API change

Automatically closed -- issue fixed for 2 weeks with no activity.