From 255ea38e5cae7ceb98e3cd7c7e0dc678b6d19f20 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Wed, 8 Jan 2014 00:40:53 -0800
Subject: [PATCH] Issue #1898464 by jenlampton, Cottser, joelpittet,
 steveoliver, Dustin Currie, shanethehat, cafuego, mr.baileys, adnen:
 toolbar.module - Convert theme_ functions to Twig.

---
 core/modules/contextual/contextual.module     |   2 +-
 .../toolbar/templates/toolbar.html.twig       |  45 +++++
 core/modules/toolbar/toolbar.api.php          |   3 +-
 core/modules/toolbar/toolbar.module           | 167 ++++++------------
 4 files changed, 99 insertions(+), 118 deletions(-)
 create mode 100644 core/modules/toolbar/templates/toolbar.html.twig

diff --git a/core/modules/contextual/contextual.module b/core/modules/contextual/contextual.module
index 47f521fbb25c..e6af59d37fd8 100644
--- a/core/modules/contextual/contextual.module
+++ b/core/modules/contextual/contextual.module
@@ -18,6 +18,7 @@ function contextual_toolbar() {
   }
 
   $tab['contextual'] = array(
+    '#type' => 'toolbar_item',
     'tab' => array(
       '#type' => 'html_tag',
       '#tag' => 'button',
@@ -28,7 +29,6 @@ function contextual_toolbar() {
         'aria-pressed' => 'false',
       ),
     ),
-    '#theme_wrappers' => array('toolbar_tab_wrapper'),
     '#wrapper_attributes' => array(
       'class' => array('hidden', 'contextual-toolbar-tab'),
     ),
diff --git a/core/modules/toolbar/templates/toolbar.html.twig b/core/modules/toolbar/templates/toolbar.html.twig
new file mode 100644
index 000000000000..bcd33640ea66
--- /dev/null
+++ b/core/modules/toolbar/templates/toolbar.html.twig
@@ -0,0 +1,45 @@
+{#
+/**
+ * @file
+ * Default theme implementation for the administrative toolbar.
+ *
+ * Available variables:
+ * - attributes: HTML attributes for the wrapper.
+ * - toolbar_attributes: HTML attributes to apply to the toolbar.
+ * - toolbar_heading: The heading or label for the toolbar.
+ * - tabs: List of tabs for the toolbar.
+ *   - attributes: HTML attributes for the tab container.
+ *   - link: Link or button for the menu tab.
+ * - trays: Toolbar tray list, each associated with a tab. Each tray in trays
+ *   contains:
+ *   - attributes: HTML attributes to apply to the tray.
+ *   - label: The tray's label.
+ *   - links: The tray menu links.
+ * - remainder: Any non-tray, non-tab elements left to be rendered.
+ *
+ * @see template_preprocess_toolbar()
+ *
+ * @ingroup themeable
+ */
+#}
+<nav{{ attributes }}>
+  <div{{ toolbar_attributes }}>
+    <h2 class="visually-hidden">{{ toolbar_heading }}</h2>
+    {% for tab in tabs %}
+      <div{{ tab.attributes }}>{{ tab.link }}</div>
+    {% endfor %}
+  </div>
+  {% for tray in trays %}
+    {% spaceless %}
+    <div{{ tray.attributes }}>
+      <div class="toolbar-lining clearfix">
+        {% if tray.label %}
+          <h3 class="toolbar-tray-name visually-hidden">{{ tray.label }}</h3>
+        {% endif %}
+        {{ tray.links }}
+      </div>
+    </div>
+    {% endspaceless %}
+  {% endfor %}
+  {{ remainder }}
+</nav>
diff --git a/core/modules/toolbar/toolbar.api.php b/core/modules/toolbar/toolbar.api.php
index 11082e92e35e..ab9b75a758b4 100644
--- a/core/modules/toolbar/toolbar.api.php
+++ b/core/modules/toolbar/toolbar.api.php
@@ -17,8 +17,7 @@
  * components.
  *
  * The toolbar provides a common styling for items denoted by the
- * .toolbar-tab class. The theme wrapper toolbar_tab_wrapper is provided to wrap
- * a toolbar item with the appropriate markup to apply the styling.
+ * .toolbar-tab class.
  *
  * The toolbar provides a construct called a 'tray'. The tray is a container
  * for content. The tray may be associated with a toggle in the administration
diff --git a/core/modules/toolbar/toolbar.module b/core/modules/toolbar/toolbar.module
index f6eaa41f9d73..c3bc4a45fd48 100644
--- a/core/modules/toolbar/toolbar.module
+++ b/core/modules/toolbar/toolbar.module
@@ -48,19 +48,12 @@ function toolbar_permission() {
 function toolbar_theme($existing, $type, $theme, $path) {
   $items['toolbar'] = array(
     'render element' => 'element',
+    'template' => 'toolbar',
   );
   $items['toolbar_item'] = array(
     'render element' => 'element',
   );
-  $items['toolbar_tab_wrapper'] = array(
-    'render element' => 'element',
-  );
-  $items['toolbar_tray_wrapper'] = array(
-    'render element' => 'element',
-  );
-  $items['toolbar_tray_heading_wrapper'] = array(
-    'render element' => 'element',
-  );
+
   return $items;
 }
 
@@ -97,13 +90,10 @@ function toolbar_element_info() {
   );
 
   // A toolbar item is wrapped in markup for common styling.  The 'tray'
-  // property contains a renderable array. theme_toolbar_tab() is a light
-  // wrapper around the l() function. The contents of tray are rendered in
-  // theme_toolbar_tab().
+  // property contains a renderable array.
   $elements['toolbar_item'] = array(
     '#pre_render' => array('toolbar_pre_render_item'),
     '#theme' => 'toolbar_item',
-    '#theme_wrappers' => array('toolbar_tab_wrapper'),
     'tab' => array(
       '#type' => 'link',
       '#title' => NULL,
@@ -218,24 +208,62 @@ function ($object) {
 }
 
 /**
- * Returns HTML that wraps the administration toolbar.
+ * Prepares variables for administration toolbar templates.
+ *
+ * Default template: toolbar.html.twig.
  *
  * @param array $variables
  *   An associative array containing:
  *   - element: An associative array containing the properties and children of
  *     the tray. Properties used: #children, #attributes and #bar.
  */
-function theme_toolbar(&$variables) {
-  if (!empty($variables['element']['#children'])) {
-    $element = $variables['element'];
-    $trays = '';
-    foreach (element_children($element) as $key) {
-      $trays .= drupal_render($element[$key]['tray']);
+function template_preprocess_toolbar(&$variables) {
+  $element = $variables['element'];
+
+  // Prepare the toolbar attributes.
+  $variables['attributes'] = $element['#attributes'];
+  $variables['toolbar_attributes'] = new Attribute($element['#bar']['#attributes']);
+  $variables['toolbar_heading'] = $element['#bar']['#heading'];
+
+  // Prepare the trays and tabs for each toolbar item as well as the remainder
+  // variable that will hold any non-tray, non-tab elements.
+  $variables['trays'] = array();
+  $variables['tabs'] = array();
+  $variables['remainder'] = array();
+  foreach (element_children($element) as $key) {
+    // Add the tray.
+    if (isset($element[$key]['tray'])) {
+      $variables['trays'][$key] = array(
+        'links' => $element[$key]['tray'],
+        'attributes' => new Attribute($element[$key]['tray']['#wrapper_attributes']),
+      );
+      if (array_key_exists('#heading', $element[$key]['tray'])) {
+        $variables['trays'][$key]['label'] = $element[$key]['tray']['#heading'];
+      }
+    }
+
+    // Pass the wrapper attributes along.
+    if (array_key_exists('#wrapper_attributes', $element[$key])) {
+      $element[$key]['#wrapper_attributes']['class'][] = 'toolbar-tab';
+      $attributes = $element[$key]['#wrapper_attributes'];
+    }
+    else {
+      $attributes = array('class' => array('toolbar-tab'));
+    }
+
+    // Add the tab.
+    $variables['tabs'][$key] = array(
+      'link' => $element[$key]['tab'],
+      'attributes' => new Attribute($attributes),
+    );
+
+    // Add other non-tray, non-tab child elements to the remainder variable for
+    // later rendering.
+    foreach (element_children($element[$key]) as $child_key) {
+      if (!in_array($child_key, array('tray', 'tab'))) {
+        $variables['remainder'][$key][$child_key] = $element[$key][$child_key];
+      }
     }
-    return '<nav' . new Attribute($element['#attributes']) . '>'
-      . '<div' . new Attribute($element['#bar']['#attributes']) . '>'
-      . '<h2 class="visually-hidden">' . $element['#bar']['#heading'] . '</h2>'
-      . $element['#children'] . '</div>' . $trays . '</nav>';
   }
 }
 
@@ -287,102 +315,11 @@ function toolbar_pre_render_item($element) {
     }
     $element['tray']['#wrapper_attributes'] += $attributes;
     $element['tray']['#wrapper_attributes']['class'][] = 'toolbar-tray';
-
-    if (!isset($element['tray']['#theme_wrappers'])) {
-      $element['tray']['#theme_wrappers'] = array();
-    }
-    // Add the standard theme_wrapper for trays.
-    array_unshift($element['tray']['#theme_wrappers'], 'toolbar_tray_wrapper');
-    // Add the theme wrapper for the tray heading.
-    array_unshift($element['tray']['#theme_wrappers'], 'toolbar_tray_heading_wrapper');
   }
 
   return $element;
 }
 
-/**
- * Implements template_preprocess_HOOK().
- */
-function template_preprocess_toolbar_tab_wrapper(&$variables) {
-  if (!isset($variables['element']['#wrapper_attributes'])) {
-    $variables['element']['#wrapper_attributes'] = array();
-  }
-  $variables['element']['#wrapper_attributes']['class'][] = 'toolbar-tab';
-}
-
-/**
- * Returns HTML for a toolbar item.
- *
- * This theme function only renders the tab portion of the toolbar item. The
- * tray portion will be rendered later.
- *
- * @param array $variables
- *   An associative array containing:
- *   - element: An associative array containing the properties and children of
- *     the tray. Property used: tab.
- *
- * @see toolbar_pre_render_item().
- * @see theme_toolbar().
- */
-function theme_toolbar_item(&$variables) {
-  return drupal_render($variables['element']['tab']);
-}
-
-/**
- * Returns HTML for wrapping a toolbar tab.
- *
- * Toolbar tabs have a common styling and placement with the toolbar's bar area.
- *
- * @param array $variables
- *   An associative array containing:
- *   - element: An associative array containing the properties and children of
- *     the tray. Properties used: #children and #attributes.
- */
-function theme_toolbar_tab_wrapper(&$variables) {
-  if (!empty($variables['element']['#children'])) {
-    $element = $variables['element'];
-    return '<div' . new Attribute($element['#wrapper_attributes']) . '>' . $element['#children'] . '</div>';
-  }
-}
-
-/**
- * Returns HTML for wrapping a toolbar tray.
- *
- * Used in combination with theme_toolbar_tab() to create an
- * association between a link tag in the administration bar and a tray.
- *
- * @param array $variables
- *   An associative array containing:
- *   - element: An associative array containing the properties and children of
- *     the tray. Properties used: #children, #toolbar_identifier and
- *     #attributes.
- */
-function theme_toolbar_tray_wrapper(&$variables) {
-  if (!empty($variables['element']['#children'])) {
-    $element = $variables['element'];
-    return '<div' . new Attribute($element['#wrapper_attributes']) . '><div class="toolbar-lining clearfix">' . $element['#children'] . '</div></div>';
-  }
-}
-
-/**
- * Returns HTML for prepending a heading to a toolbar tray.
- *
- * @param array $variables
- *   An associative array containing:
- *   - element: An associative array containing the properties and children of
- *     the tray. Properties used: #children and #heading.
- */
-function theme_toolbar_tray_heading_wrapper(&$variables) {
-  $element = $variables['element'];
-  if (!empty($element['#children'])) {
-    $heading = '';
-    if (!empty($element['#heading'])) {
-      $heading = '<h3 class="toolbar-tray-name visually-hidden">' . $element['#heading'] . '</h3>';
-    }
-    return $heading . $element['#children'];
-  }
-}
-
 /**
  * Implements hook_toolbar().
  */
-- 
GitLab