Index: filefield_sources.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/filefield_sources/filefield_sources.module,v
retrieving revision 1.4
diff -u -r1.4 filefield_sources.module
--- filefield_sources.module	27 Oct 2009 19:50:10 -0000	1.4
+++ filefield_sources.module	20 Nov 2009 09:17:13 -0000
@@ -26,23 +26,9 @@
  * Implementation of hook_elements().
  */
 function filefield_sources_elements() {
-  $params = array();
-  $sources = filefield_sources_invoke_all('info', $params);
-
-  // Build a list of all the extensions for FileField.
   $extras = array();
-  foreach ($sources as $source) {
-    if (isset($source['process'])) {
-      $extras['#process'][] = $source['process'];
-    }
-    if (isset($source['validate'])) {
-      $extras['#element_validate'][] = $source['validate'];
-    }
-    if (isset($source['value'])) {
-      $extras['#filefield_value_callback'][] = $source['value'];
-    }
-  }
   $extras['#process'][] = 'filefield_sources_process';
+  $extras['#filefield_value_callback'][] = 'filefield_sources_value';
 
   // Add the extra processing to both FileField and ImageField widgets.
   return array(
@@ -68,12 +54,43 @@
 /**
  * Implementation of hook_widget_settings_alter().
  *
- * @todo Get this hook working. See http://drupal.org/node/417122
  */
-function filefield_sources_widget_settings_alter(&$field, $op) {
-  // Add settings to the FileField widget form.
-  $params = array(&$field, $op);
-  return filefield_sources_invoke_all('settings', $params);
+function filefield_sources_widget_settings_alter(&$settings, $op, $widget) {
+  // @todo: is the following check sensible?
+  $widget_type = isset($widget['widget_type']) ? $widget['widget_type'] : $widget['type'];
+  if (!in_array($widget_type, array_keys(array('imagefield_widget', 'filefield_widget')))) {
+    return;
+  }
+  $sources = filefield_sources_filefield_sources_info();
+  if ($op == 'save') {
+    $sources_enabled = array();
+    foreach ($sources as $source => $source_info) {
+      $sources_enabled[] = 'sources_'.$source.'_enabled';
+    }
+    $settings = array_merge($settings, $sources_enabled);
+  }
+  if ($op == 'form') {
+    // Add fieldset for sources to the FileField widget form.
+    $settings['filefield_sources'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Filefield Sources'),
+      '#collapsible' => TRUE,
+      '#collapsed' => TRUE,
+      '#description' => t('These options allow the user to choose which sources are activated when uploading a file.'),
+      '#weight' => 15,
+    );
+    // add a checkbox to enable or disable every source per widget
+    foreach ($sources as $source => $source_info) {
+      $enabled = (isset($widget['sources_'.$source.'_enabled'])) ? (bool)$widget['sources_'.$source.'_enabled'] : true;
+      $settings['filefield_sources']['sources_'.$source.'_enabled'] = array(
+        '#type' => 'checkbox',
+        '#title' => $source_info['name'],
+        '#default_value' => $enabled,
+      );
+    }
+  }
+  $params = array(&$settings, $op, $widget);
+  filefield_sources_invoke_all('settings', $params);
 }
 
 /**
@@ -91,6 +108,21 @@
   drupal_add_css($path .'/filefield_sources.css');
   drupal_add_js($path .'/filefield_sources.js');
 
+  // Either add the property here, or check for it's existence in the 
+  // behaviours. Otherwise you get errors when no source defines hint texts.
+  drupal_add_js(array('fileFieldSources' => array()), 'setting');
+  
+  $info = filefield_sources_info();
+  $field = content_fields($element['#field_name'], $element['#type_name']);
+  foreach ($info as $name => $source) {
+    if (!isset($field['widget']['sources_'.$name.'_enabled']) || $field['widget']['sources_'.$name.'_enabled'] == true) {
+      if (isset($source['process']) && function_exists($source['process'])) {
+        $params = array($element, $edit, &$form_state, $form);
+        $element = call_user_func_array($source['process'], $params);
+      }
+    }
+  }
+
   // Check the element for hint text that might need to be added.
   foreach (element_children($element) as $key) {
     if (isset($element[$key]['#filefield_sources_hint_text']) && !isset($js_added[$key])) {
@@ -101,7 +133,6 @@
   }
 
   // Add the list of sources to the element for toggling between sources.
-  $info = filefield_sources_info();
   if (empty($element['fid']['#value'])) {
     $element['filefield_sources_list'] = array(
       '#type' => 'markup',
@@ -113,6 +144,22 @@
   return $element;
 }
 
+/**
+ * A #filefield_value_callback function.
+ */
+function filefield_sources_value($element, &$item) {
+  $info = filefield_sources_info();
+  $field = content_fields($element['#field_name'], $element['#type_name']);
+  foreach ($info as $name => $source) {
+    if (!isset($field['widget']['sources_'.$name.'_enabled']) || $field['widget']['sources_'.$name.'_enabled'] == true) {
+      if (isset($source['value']) && function_exists($source['value'])) {
+        $params = array($element, &$item);
+        call_user_func_array($source['value'], $params);
+      }
+    }
+  }
+}
+
 function filefield_sources_invoke_all($method, &$params) {
   $return = array();
   foreach (filefield_sources_includes() as $source) {
@@ -173,7 +220,7 @@
  */
 function theme_filefield_sources_list($element, $sources) {
   $links = array();
-
+  $field = content_fields($element['#field_name'], $element['#type_name']);
   // Add the default "Upload" since it's not in our list.
   $default['upload'] = array(
     'label' => t('Upload'),
@@ -182,7 +229,9 @@
   $sources = array_merge($default, $sources);
 
   foreach ($sources as $name => $source) {
-    $links[] = '<a href="#" onclick="return false;" title="' . $source['description'] . '" id="' . $element['#id'] . '-' . $name . '-source" class="filefield-source filefield-source-' . $name . '">' . $source['label'] . '</a>';
+    if (!isset($field['widget']['sources_'.$name.'_enabled']) || $field['widget']['sources_'.$name.'_enabled']) {
+      $links[] = '<a href="#" onclick="return false;" title="' . $source['description'] . '" id="' . $element['#id'] . '-' . $name . '-source" class="filefield-source filefield-source-' . $name . '">' . $source['label'] . '</a>';
+    }
   }
   return '<div class="filefield-sources-list">' . implode(' | ', $links) . '</div>';
 }
Index: sources/imce.inc
===================================================================
RCS file: /cvs/drupal/contributions/modules/filefield_sources/sources/imce.inc,v
retrieving revision 1.2
diff -u -r1.2 imce.inc
--- sources/imce.inc	5 May 2009 18:41:24 -0000	1.2
+++ sources/imce.inc	19 Nov 2009 15:38:09 -0000
@@ -50,13 +50,14 @@
 /**
  * Implementation of hook_filefield_source_settings().
  */
-function filefield_source_imce_settings($op, $field) {
-  $return = array();
-
-  // Add settings to the FileField widget form.
-
-  return $return;
-
+function filefield_source_imce_settings(&$settings, $op, $widget) {
+  if ($op == 'form') {
+    // Add settings to the FileField widget form.
+  }
+  if ($op == 'save') {
+    // save settings to the Filefield widget
+    //$settings = array_merge($settings, array('example'));
+  }
 }
 
 /**
Index: sources/reference.inc
===================================================================
RCS file: /cvs/drupal/contributions/modules/filefield_sources/sources/reference.inc,v
retrieving revision 1.1
diff -u -r1.1 reference.inc
--- sources/reference.inc	17 Apr 2009 00:20:14 -0000	1.1
+++ sources/reference.inc	19 Nov 2009 15:38:19 -0000
@@ -63,13 +63,14 @@
 /**
  * Implementation of hook_filefield_source_settings().
  */
-function filefield_source_reference_settings($op, $field) {
-  $return = array();
-
-  // Add settings to the FileField widget form.
-
-  return $return;
-
+function filefield_source_reference_settings(&$settings, $op, $widget) {
+  if ($op == 'form') {
+    // Add settings to the FileField widget form.
+  }
+  if ($op == 'save') {
+    // save settings to the Filefield widget
+    //$settings = array_merge($settings, array('example'));
+  }
 }
 
 /**
Index: sources/remote.inc
===================================================================
RCS file: /cvs/drupal/contributions/modules/filefield_sources/sources/remote.inc,v
retrieving revision 1.2
diff -u -r1.2 remote.inc
--- sources/remote.inc	20 Sep 2009 20:57:20 -0000	1.2
+++ sources/remote.inc	19 Nov 2009 15:38:56 -0000
@@ -58,13 +58,14 @@
 /**
  * Implementation of hook_filefield_source_settings().
  */
-function filefield_source_remote_settings($op, $field) {
-  $return = array();
-
-  // Add settings to the FileField widget form.
-
-  return $return;
-
+function filefield_source_remote_settings(&$settings, $op, $widget) {
+  if ($op == 'form') {
+    // Add settings to the FileField widget form.
+  }
+  if ($op == 'save') {
+    // save settings to the Filefield widget
+    //$settings = array_merge($settings, array('example'));
+  }
 }
 
 /**

