From 1d9e77a513d02d0922af8d207edf44a7acff08cc Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Wed, 8 Dec 2010 06:55:01 +0000
Subject: [PATCH] #988760 by ksenzee: Fixed theme_container can't be used with
 non-form elements

---
 includes/form.inc | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/includes/form.inc b/includes/form.inc
index 3aae8cfa4755..3d69b4433c7e 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -1700,6 +1700,9 @@ function form_builder($form_id, &$element, &$form_state) {
     else {
       $form_state['process_input'] = FALSE;
     }
+
+    // All form elements should have an #array_parents property.
+    $element['#array_parents'] = array();
   }
 
   if (!isset($element['#id'])) {
@@ -1756,7 +1759,7 @@ function form_builder($form_id, &$element, &$form_state) {
       $element[$key]['#parents'] = $element[$key]['#tree'] && $element['#tree'] ? array_merge($element['#parents'], array($key)) : array($key);
     }
     // Ensure #array_parents follows the actual form structure.
-    $array_parents = isset($element['#array_parents']) ? $element['#array_parents'] : array();
+    $array_parents = $element['#array_parents'];
     $array_parents[] = $key;
     $element[$key]['#array_parents'] = $array_parents;
 
@@ -3065,7 +3068,11 @@ function form_process_container($element, &$form_state) {
 }
 
 /**
- * Returns HTML for a container for grouped form items.
+ * Returns HTML to wrap child elements in a container.
+ *
+ * Used for grouped form items. Can also be used as a #theme_wrapper for any
+ * renderable element, to surround it with a <div> and add attributes such as
+ * classes or an HTML id.
  *
  * @param $variables
  *   An associative array containing:
@@ -3076,11 +3083,17 @@ function form_process_container($element, &$form_state) {
  */
 function theme_container($variables) {
   $element = $variables['element'];
-  if (!isset($element['#attributes']['id'])) {
-    $element['#attributes']['id'] = $element['#id'];
+
+  // Special handling for form elements.
+  if (isset($element['#array_parents'])) {
+    // Assign an html ID.
+    if (!isset($element['#attributes']['id'])) {
+      $element['#attributes']['id'] = $element['#id'];
+    }
+    // Add the 'form-wrapper' class.
+    $element['#attributes']['class'][] = 'form-wrapper';
   }
-  // Force the 'form-wrapper' class.
-  $element['#attributes']['class'][] = 'form-wrapper';
+
   return '<div' . drupal_attributes($element['#attributes']) . '>' . $element['#children'] . '</div>';
 }
 
-- 
GitLab