#600974: try to make sense out of the theme() function $variables argument.

From: damz <damz@devland.local>


---
 common.inc |    4 ++--
 theme.inc  |   24 ++++++++----------------
 2 files changed, 10 insertions(+), 18 deletions(-)

diff --git includes/common.inc includes/common.inc
index 33c0966..5bf3bc7 100644
--- includes/common.inc
+++ includes/common.inc
@@ -4490,7 +4490,7 @@ function drupal_render(&$elements) {
   // Call the element's #theme function if it is set. Then any children of the
   // element have to be rendered there.
   if (isset($elements['#theme'])) {
-    $elements['#children'] = theme($elements['#theme'], $elements);
+    $elements['#children'] = theme($elements['#theme'], array('element' => $elements));
   }
   // If #theme was not set and the element has children, render them now
   // using drupal_render_children().
@@ -4502,7 +4502,7 @@ function drupal_render(&$elements) {
   // children.
   if (isset($elements['#theme_wrappers'])) {
     foreach ($elements['#theme_wrappers'] as $theme_wrapper) {
-      $elements['#children'] = theme($theme_wrapper, $elements);
+      $elements['#children'] = theme($theme_wrapper, array('element' => $elements));
     }
   }
 
diff --git includes/theme.inc includes/theme.inc
index e570302..40a57a1 100644
--- includes/theme.inc
+++ includes/theme.inc
@@ -778,26 +778,18 @@ function theme($hook, $variables = array()) {
     }
   }
 
-  // If a renderable array is passed as $variables, then set $variables to
+  // If $variables only contains a renderable $elements, then set $variables to
   // what's expected by the theme hook. If the theme hook expects a single
-  // argument, set the renderable array as that argument. If the theme hook
-  // expects multiple arguments, set the properties of the renderable array as
+  // 'elements' argument, set the renderable array as that argument. If the theme
+  // hook expects multiple arguments, set the properties of the renderable array as
   // those arguments.
-  if (isset($variables['#theme']) || isset($variables['#theme_wrappers'])) {
-    $element = $variables;
-    $variables = array();
-    $n = count($info['arguments']);
-    if ($n == 1) {
-      $arg_keys = array_keys($info['arguments']);
-      $variables[$arg_keys[0]] = $element;
-    }
-    elseif ($n > 1) {
-      foreach ($info['arguments'] as $name => $default) {
-        if (isset($element["#$name"])) {
-          $variables[$name] = $element["#$name"];
-        }
+  if ((array_keys($variables) === array('elements')) && ($info['arguments'] !== array('elements'))) {
+    foreach ($info['arguments'] as $name => $default) {
+      if (isset($variables['elements']["#$name"])) {
+        $variables[$name] = $variables['elements']["#$name"];
       }
     }
+    unset($variables['elements']);
   }
 
   // Merge in argument defaults.
