From 2df6695f87b81b9c11081f6d8606e0db2e075a09 Mon Sep 17 00:00:00 2001
From: Brandon Bergren <bdragon@rtk0.net>
Date: Fri, 24 May 2013 17:34:58 -0500
Subject: [PATCH]  #1843678: Make messages renderable.

---
 core/includes/theme.inc                            |   45 +++++++-------------
 .../system/templates/status-messages.html.twig     |   32 ++++++++++++++
 2 files changed, 47 insertions(+), 30 deletions(-)
 create mode 100644 core/modules/system/templates/status-messages.html.twig

diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index 782b562..c92b015 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -1703,33 +1703,15 @@ function theme_datetime($variables) {
  *   - display: (optional) Set to 'status' or 'error' to display only messages
  *     of that type.
  */
-function theme_status_messages($variables) {
-  $display = $variables['display'];
-  $output = '';
-
-  $status_heading = array(
-    'status' => t('Status message'),
-    'error' => t('Error message'),
-    'warning' => t('Warning message'),
-  );
-  foreach (drupal_get_messages($display) as $type => $messages) {
-    $output .= "<div class=\"messages messages--$type\">\n";
-    if (!empty($status_heading[$type])) {
-      $output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n";
-    }
-    if (count($messages) > 1) {
-      $output .= " <ul>\n";
-      foreach ($messages as $message) {
-        $output .= '  <li>' . $message . "</li>\n";
-      }
-      $output .= " </ul>\n";
-    }
-    else {
-      $output .= $messages[0];
+function template_preprocess_status_messages(&$variables) {
+  // Convert messages array to renderable format.
+  foreach (drupal_get_messages($variables['display']) as $type => $messages) {
+    foreach ($messages as $message) {
+      $variables['messages'][$type][] = array(
+        '#markup' => $message,
+      );
     }
-    $output .= "</div>\n";
   }
-  return $output;
 }
 
 /**
@@ -2942,6 +2924,13 @@ function template_preprocess_page(&$variables) {
   if ($suggestions = theme_get_suggestions(arg(), 'page')) {
     $variables['theme_hook_suggestions'] = $suggestions;
   }
+
+  // Populate messages last in order to capture as many as possible for the
+  // current page.
+  $variables['messages'] = array(
+    '#theme' => 'status_messages',
+    '#access' => $variables['show_messages'],
+  );
 }
 
 /**
@@ -2967,11 +2956,6 @@ function template_process_page(&$variables) {
     $variables['title'] = drupal_get_title();
   }
 
-  // Generate messages last in order to capture as many as possible for the
-  // current page.
-  if (!isset($variables['messages'])) {
-    $variables['messages'] = $variables['show_messages'] ? array('#theme' => 'status_messages') : '';
-  }
 }
 
 /**
@@ -3240,6 +3224,7 @@ function drupal_common_theme() {
     ),
     'status_messages' => array(
       'variables' => array('display' => NULL),
+      'template' => 'status-messages',
     ),
     'link' => array(
       'variables' => array('text' => NULL, 'path' => NULL, 'options' => array()),
diff --git a/core/modules/system/templates/status-messages.html.twig b/core/modules/system/templates/status-messages.html.twig
new file mode 100644
index 0000000..d977e30
--- /dev/null
+++ b/core/modules/system/templates/status-messages.html.twig
@@ -0,0 +1,32 @@
+{#
+/**
+ * @file
+ * Default theme implementation for status messages.
+ *
+ * Available variables:
+ * - messages: An array of The messages to output.
+ *
+ * @see template_preprocess()
+ * @see template_preprocess_status_messages()
+ *
+ * @ingroup themeable
+ */
+#}
+{% set status_heading = {'status': 'Status message'|t, 'error': 'Error message'|t, 'warning': 'Warning message'|t} %}
+{% for type, message_list in messages if message_list %}
+  <div class="messages messages--{{ type }}">
+  {% if attribute(status_heading, type) %}
+    <h2 class="element-invisible">{{ attribute(status_heading, type) }}</h2>
+  {% endif %}
+
+  {% if message_list|length > 1 %}
+    <ul>
+    {% for message in message_list %}
+      <li>{{ message }}</li>
+    {% endfor %}
+    </ul>
+  {% else %}
+    {{ message_list }}
+  {% endif %}
+  </div>
+{% endfor %}
-- 
1.7.10.4

