From 416676e793cfbd5813b040dbb3487aa92fd05f5d Mon Sep 17 00:00:00 2001
From: solotandem <jim@boombatower.com>
Date: Mon, 28 Nov 2011 14:08:17 -0600
Subject: [PATCH] #1355082: Recurse the form elements array to handle nested fieldsets.

---
 domain_conf/domain_conf.module |   41 ++++++++++++++++++++++++++++++---------
 1 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/domain_conf/domain_conf.module b/domain_conf/domain_conf.module
index ea9403c..fd7dd7e 100644
--- a/domain_conf/domain_conf.module
+++ b/domain_conf/domain_conf.module
@@ -554,15 +554,17 @@ function domain_conf_domainupdate($op, $domain, $form_state = array()) {
 }
 
 /**
- * Retrieves elements from hook_domainconf() and formats them
- * as needed.
+ * Retrieves elements from hook_domainconf() and formats them as needed.
  *
  * @param $all
  *   Should the function return all hook implementations or just those marked
  *   with the domain_settings flag.  Defaults to FALSE.  Used to determine if
  *   we are loading configuration options specific to the Domain Access module.
+ * @param $settings
+ *   Array of settings for the domain being configured.
+ *
  * @return
- *   An array of form elements according to the FormsAPI or an empty array.
+ *   Array of form elements with default values set, or an empty array.
  */
 function domain_conf_api($all = FALSE, $settings = array()) {
   global $_domain;
@@ -570,16 +572,12 @@ function domain_conf_api($all = FALSE, $settings = array()) {
   $extra = module_invoke_all('domainconf', $_domain);
   if (!empty($extra)) {
     foreach ($extra as $key => $value) {
-      foreach (element_children($value) as $element) {
-        if (isset($value[$element]['#default_value']) && isset($settings[$element])) {
-          $value[$element]['#default_value'] = $settings[$element];
-        }
-      }
-      if ($value['#domain_setting'] == TRUE || $all == TRUE) {
+      // The '#domain_setting' key only applies to a top-level form element.
+      if (!empty($value['#domain_setting']) || $all == TRUE) {
         // Discard the #domain_setting flag; it is not needed.
         unset($value['#domain_setting']);
         // Set the $options array.
-        $options[$key] = $value;
+        $options[$key] = domain_conf_settings_load($value, $settings);
       }
     }
   }
@@ -587,6 +585,29 @@ function domain_conf_api($all = FALSE, $settings = array()) {
 }
 
 /**
+ * Recursively set default values of form fields based on settings for the domain being configured.
+ *
+ * @param $elements
+ *   Array of form elements.
+ * @param $settings
+ *   Array of settings for the domain being configured.
+ *
+ * @return
+ *   Array of form elements with default values set.
+ */
+function domain_conf_settings_load($elements, $settings) {
+  foreach (element_children($elements) as $key) {
+    if (isset($elements[$key]) && $elements[$key]) {
+      $elements[$key] = domain_conf_settings_load($elements[$key], $settings);
+    }
+    if (array_key_exists('#default_value', $elements[$key]) && isset($settings[$key])) {
+      $elements[$key]['#default_value'] = $settings[$key];
+    }
+  }
+  return $elements;
+}
+
+/**
  * Implement hook_block().
  *
  * The primary links and secondary links blocks do not respect our settings,
-- 
1.7.3.4

