I add my own validations to forms, for example the user registration. The errors I set with form_set_error are added below the drupal generated errors. I would like to show them in the same order of my form fields on the page.

I thought I can use theme_status_messages($variables). When I do a dpm($variables) on a from with status messages it just shows an empty array.

  • As theme_status_messages() is used from Drupal, and the messages are shown, the function clearly works. How are you overriding that theme function? – apaderno Nov 5 '11 at 21:54
  • I add this to my bartik/template.php: function bartik_status_messages(&$variables) { var_dump($variables); }. It shows "array(1) { ["display"]=> NULL }" and the status message isn't shown. If I remove it it shows the messages from the previous page. Found this related link drupal.org/node/1049648 but still can't solve it. Thank you! – uwe Nov 5 '11 at 23:46

$variables['display'], passed to the theme function is optional, and its purpose is only to limit the type of messages being shown; for example, if its value is "error," only the error messages should be shown. The list of messages is returned from drupal_get_messages(), which should be called as drupal_get_messages($variables['display']).

To answer to your comment, you can avoid duplicating all the code used by theme_status_messages() if you follow these steps:

  • obtain the array of messages from drupal_get_messages()
  • sort the obtained array of messages
  • use the array to set the content of $_SESSION['messages'], which is the session variable used to contain all the messages set from the modules
  • call theme_status_messages(), passing the same argument passed to your theme function (not the array obtained from drupal_get_messages())

This method seems half of a hack, to me. What happens if Drupal would change how the list of messages are saved? It could be that future Drupal versions will not use a session variable for this purpose.
It also take the assumption that other modules didn't override the theme function, but it would be easy in the implementation of hook_theme_registry_alter() to save the previously defined theme function, and call that function instead of assuming that function is theme_status_messages().

  • thank you. I can now get the array of status messages and display them. I was hoping I can just sort the array and return it to be rendered by drupal's default status message function. Looks like I have to copy all the code from api.drupal.org/api/drupal/includes--theme.inc/function/… to display the HTML myself. – uwe Nov 6 '11 at 17:39
  • I expanded my answer to reply to your comment. – apaderno Nov 6 '11 at 17:48

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.