From 02d9040085d54aae8539533cd4e1c97ddaa33626 Mon Sep 17 00:00:00 2001
From: Capi Etheriel <barraponto@gmail.com>
Date: Wed, 26 Mar 2014 19:02:49 -0300
Subject: [PATCH] Issue #2223365 by barraponto: use Bootstrap's input group for
 text format.

---
 includes/form.inc  | 32 ++++++++++++++++++++
 includes/theme.inc | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 119 insertions(+)

diff --git a/includes/form.inc b/includes/form.inc
index 9f31f9a..991b796 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -35,6 +35,10 @@ function radix_element_info_alter(&$elements) {
     if (!empty($element['#input'])) {
       $element['#process'][] = '_radix_process_input';
     }
+    // Process text format elements.
+    if (!empty($element['#type']) && $element['#type'] == 'text_format') {
+      $element['#after_build'][] = '_radix_text_format_after_build';
+    }
   }
 }
 
@@ -80,6 +84,34 @@ function _radix_process_input(&$element, &$form_state) {
 }
 
 /**
+ * Last minute theming for format selectors.
+ */
+function _radix_text_format_after_build($element, &$form_state) {
+  // This thing runs twice, but the first time around format is not set yet.
+  if (empty($element['format'])) { return $element; }
+
+  $format = &$element['format']['format'];
+  // Set title to show as a field_prefix if there is no field_prefix set.
+  $format['#field_prefix'] = empty($format['#field_prefix']) ?
+    $format['#title'] : $format['#field_prefix'];
+  unset($format['#title']);
+
+  $format['#theme_wrappers'] = array_map(
+    function($function) {
+      // Overrides only form_element wrapper.
+      if ($function == 'form_element') {
+        return 'input_group';
+      }
+      // But leaves the other wrappers in place.
+      else {
+        return $function;
+      }
+    },
+    $format['#theme_wrappers']);
+  return $element;
+}
+
+/**
  * Implements theme_form_element().
  */
 function radix_form_element($variables) {
diff --git a/includes/theme.inc b/includes/theme.inc
index 41b1a36..0fb8055 100644
--- a/includes/theme.inc
+++ b/includes/theme.inc
@@ -33,6 +33,14 @@ function radix_theme(&$existing, $type, $theme, $path) {
       ),
       'function' => 'theme_radix_button_dropdown',
     ),
+    'input_group' => array(
+      'render element' => 'element',
+    ),
+    'input_group_addon' => array(
+      'variables' => array(
+        'content' => NULL,
+      ),
+    )
   );
 }
 
@@ -214,3 +222,82 @@ function theme_radix_button_dropdown($variables) {
   $output .= '</div>';
   return $output;
 }
+
+/**
+ * Implements template_preprocess_input_group().
+ */
+function radix_preprocess_input_group(&$variables) {
+  $element = &$variables['element'];
+
+  // Add element #id for #type 'item'.
+  if (isset($element['#markup']) && !empty($element['#id'])) {
+    $attributes['id'] = $element['#id'];
+  }
+  // Add element's #type and #name as class to aid with JS/CSS selectors.
+  $attributes['class'] = array('form-item');
+  if (!empty($element['#type'])) {
+    $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
+  }
+  if (!empty($element['#name'])) {
+    $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
+  }
+  // Add a class for disabled elements to facilitate cross-browser styling.
+  if (!empty($element['#attributes']['disabled'])) {
+    $attributes['class'][] = 'form-disabled';
+  }
+  // Add error class.
+  if (isset($element['#parents']) && form_get_error($element) !== NULL && !empty($element['#validated'])) {
+    $attributes['class'][] = 'has-error';
+  }
+  // Add the essential input-group class.
+  $attributes['class'][] = 'input-group';
+
+  $variables['attributes'] = $attributes;
+
+  // This function is invoked as theme wrapper, but the rendered form element
+  // may not necessarily have been processed by form_builder().
+  $element += array(
+    '#title_display' => 'before',
+  );
+  // If #title is not set, we don't display any label or required marker.
+  if (!isset($element['#title'])) {
+    $element['#title_display'] = 'none';
+  }
+}
+
+/**
+ * Implements theme_input_group().
+ */
+function radix_input_group($variables) {
+  $element = $variables['element'];
+
+  $output = '<div' . drupal_attributes($variables['attributes']) . '>' . "\n";
+
+  if (in_array($element['#title_display'], array('hidden', 'before'))) {
+    $output .= theme('form_element_label', $variables);
+  }
+
+  $output .= empty($element['#field_prefix']) ? '' : theme('input_group_addon', array('content' => $element['#field_prefix']));
+  $output .= $element['#children'];
+  $output .= empty($element['#field_suffix']) ? '' : theme('input_group_addon', array('content' => $element['#field_suffix']));
+
+
+  if ($element['#title_display'] == 'after') {
+    $output .= theme('form_element_label', $variables);
+  }
+
+  if (!empty($element['#description'])) {
+    $output .= '<span class="help-block">' . $element['#description'] . "</span>\n";
+  }
+
+  $output .= "</div>\n";
+
+  return $output;
+}
+
+/**
+ * Implements theme_input_group_addon().
+ */
+function radix_input_group_addon($variables) {
+  return '<span class="input-group-addon">' . $variables['content'] . '</span>';
+}
-- 
1.9.1

