Index: includes/authorize.inc =================================================================== RCS file: /Users/wright/drupal/local_repo/drupal/includes/authorize.inc,v retrieving revision 1.3 diff -u -p -r1.3 authorize.inc --- includes/authorize.inc 27 Oct 2009 03:27:00 -0000 1.3 +++ includes/authorize.inc 27 Oct 2009 23:30:41 -0000 @@ -81,8 +81,7 @@ function authorize_filetransfer_form($fo '#title' => t('@backend connection settings', array('@backend' => $backend['title'])), ); - $current_settings = variable_get('authorize_filetransfer_connection_settings_' . $name, array()); - $form['connection_settings'][$name] += system_get_filetransfer_settings_form($name, $current_settings); + $form['connection_settings'][$name] += _authorize_filetransfer_connection_settings($name, $backend); // Start non-JS code. if (isset($form_state['values']['connection_settings']['authorize_filetransfer_default']) && $form_state['values']['connection_settings']['authorize_filetransfer_default'] == $name) { @@ -116,6 +115,62 @@ function authorize_filetransfer_form($fo } /** + * Generate the Form API array for the settings for a given connection backend. + * + * @param $backend + * The name of the backend (e.g. 'ftp', 'ssh', etc). + * @param array $info + * The array of information about the backend from the info hook. + * @return + * Form API array of connection settings for the given backend. + * + * @see hook_filetransfer_backends() + */ +function _authorize_filetransfer_connection_settings($backend, array $info) { + $defaults = variable_get('authorize_filetransfer_connection_settings_' . $backend, array()); + // Invoke the callback to get the settings form. + $form = call_user_func($info['settings_form']); + // Fill in the defaults based on the saved settings, if any. + _authorize_filetransfer_connection_settings_set_defaults($form, NULL, $defaults); + return $form; +} + +/** + * Recursively fill in the default settings on a file transfer connection form. + * + * The default settings for the file transfer connection forms are saved in + * the database. The settings are stored as a nested array in the case of a + * settings form that has fieldsets or otherwise uses a nested structure. + * Therefore, to properly add defaults, we need to walk through all the + * children form elements and process those defaults recursively. + * + * @param &$element + * Reference to the Form API form element we're operating on. + * @param $key + * The key for our current form element, if any. + * @param array $defaults + * The default settings for the file transfer backend we're operating on. + * @return + * Nothing, this function just sets $element['#default_value'] if needed. + */ +function _authorize_filetransfer_connection_settings_set_defaults(&$element, $key, array $defaults) { + // If we're operating on a form element which isn't a fieldset, and we have + // a default setting saved, stash it in #default_value. + if (!empty($key) && isset($defaults[$key]) && isset($element['#type']) && $element['#type'] != 'fieldset') { + $element['#default_value'] = $defaults[$key]; + } + // Now, we walk through all the child elements, and recursively invoke + // ourself on each one. Since the $defaults settings array can be nested + // (because of #tree, any values inside fieldsets will be nested), if + // there's a subarray of settings for the form key we're currently + // processing, pass in that subarray to the recursive call. Otherwise, just + // pass on the whole $defaults array. + foreach (element_children($element) as $child_key) { + _authorize_filetransfer_connection_settings_set_defaults($element[$child_key], $child_key, ((isset($defaults[$key]) && is_array($defaults[$key])) ? $defaults[$key] : $defaults)); + } +} + +/** * Validate callback for the filetransfer authorization form. * * @see authorize_filetransfer_form() Index: modules/system/system.module =================================================================== RCS file: /Users/wright/drupal/local_repo/drupal/modules/system/system.module,v retrieving revision 1.827 diff -u -p -r1.827 system.module --- modules/system/system.module 27 Oct 2009 04:16:39 -0000 1.827 +++ modules/system/system.module 27 Oct 2009 21:15:06 -0000 @@ -1543,27 +1543,6 @@ function system_filetransfer_backends() } /** - * Helper function to return a form for configuring a filetransfer backend. - * - * @param string $filetransfer_backend_name - * The name of the backend to return a form for. - * - * @param string $defaults - * An associative array of settings to pre-populate the form with. - */ -function system_get_filetransfer_settings_form($filetransfer_backend_name, $defaults) { - $available_backends = module_invoke_all('filetransfer_backends'); - $form = call_user_func($available_backends[$filetransfer_backend_name]['settings_form']); - - foreach ($form as $name => &$element) { - if (isset($defaults[$name])) { - $element['#default_value'] = $defaults[$name]; - } - } - return $form; -} - -/** * Returns the form to configure the filetransfer class for FTP */ function system_filetransfer_backend_form_ftp() {