? 436174-new.patch
Index: filefield_sources.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/filefield_sources/filefield_sources.module,v
retrieving revision 1.4
diff -u -p -r1.4 filefield_sources.module
--- filefield_sources.module	27 Oct 2009 19:50:10 -0000	1.4
+++ filefield_sources.module	3 Jan 2010 20:16:15 -0000
@@ -26,23 +26,9 @@ function filefield_sources_init() {
  * 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,47 @@ function filefield_sources_theme() {
 /**
  * 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) {
+  $widget_type = isset($widget['widget_type']) ? $widget['widget_type'] : $widget['type'];
+  if (!in_array($widget_type, 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,
+    );
+    foreach ($sources as $source => $source_info) {
+      $enabled = (isset($widget['sources_'.$source.'_enabled'])) ? (bool)$widget['sources_'.$source.'_enabled'] : true;
+      $settings['filefield_sources']['sources_'.$source] = array(
+        '#title' => $source_info['name'],
+        '#type' => 'fieldset',
+        '#collapsible' => TRUE,
+        '#collapsed' => TRUE,
+      );
+      $settings['filefield_sources']['sources_'.$source]['sources_'.$source.'_enabled'] = array(
+       '#type' => 'checkbox',
+       '#title' => t('Enabled'),
+       '#default_value' => $enabled,
+      );
+    }
+  }
+  $params = array(&$settings, $op, $widget);
+  filefield_sources_invoke_all('settings', $params);
 }
 
 /**
@@ -91,6 +112,21 @@ function filefield_sources_process($elem
   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 +137,6 @@ function filefield_sources_process($elem
   }
 
   // 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 +148,22 @@ function filefield_sources_process($elem
   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 +224,7 @@ function filefield_sources_includes($inc
  */
 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 +233,9 @@ function theme_filefield_sources_list($e
   $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-contrib/contributions/modules/filefield_sources/sources/imce.inc,v
retrieving revision 1.2
diff -u -p -r1.2 imce.inc
--- sources/imce.inc	5 May 2009 18:41:24 -0000	1.2
+++ sources/imce.inc	3 Jan 2010 20:16:15 -0000
@@ -50,13 +50,14 @@ function filefield_source_imce_theme() {
 /**
  * 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-contrib/contributions/modules/filefield_sources/sources/reference.inc,v
retrieving revision 1.1
diff -u -p -r1.1 reference.inc
--- sources/reference.inc	17 Apr 2009 00:20:14 -0000	1.1
+++ sources/reference.inc	3 Jan 2010 20:16:15 -0000
@@ -63,13 +63,26 @@ function filefield_source_reference_them
 /**
  * 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') {
+    if (!empty($settings['filefield_sources']['sources_reference'])) {
+      //Add settings to the FileField widget form.
+      foreach (content_fields() as $field) {
+        if ($field['type'] == 'filefield') {
+          $reference_options[$field['field_name']] = t($field['field_name']);
+        }
+      }
+      $settings['filefield_sources']['sources_reference']['referenceable_fields'] = array(
+        '#title' => t('Referenceable Fields'),
+        '#type' => 'checkboxes',
+        '#options' => $reference_options,
+        '#default_value' => $widget['referenceable_fields'], 
+      );
+    }
+  }
+  if ($op == 'save') {
+    $settings = array_merge($settings, array('referenceable_fields'));
+  }
 }
 
 /**
@@ -173,7 +186,8 @@ function theme_filefield_source_referenc
  *   Optional. A CCK field array for which to filter returned files.
  */
 function filefield_source_reference_get_files($filename, $field = NULL) {
-  if (!isset($field)) {
+$fields = array();
+  if (!isset($field['widget']['referenceable_fields'])) {
     foreach (content_fields() as $field) {
       if ($field['type'] == 'filefield') {
         $fields[] = $field;
@@ -181,15 +195,22 @@ function filefield_source_reference_get_
     }
   }
   else {
-    $fields = array($field);
+    foreach ($field['widget']['referenceable_fields'] as $field_name => $enabled) {
+      if ($enabled != '0') {
+        $fields[] = content_fields($field_name);
+      }
+    }
   }
-
+  unset($field);
   $files = array();
-  foreach ($fields as $field) {
-    $db_info = content_database_info($field);
-    $result = db_query("SELECT f.* FROM {" . $db_info['table'] . "} c INNER JOIN {files} f ON c." . $db_info['columns']['fid']['column'] . " = f.fid WHERE f.filename LIKE '%s%%' AND status = 1", $filename);
-    while ($file = db_fetch_object($result)) {
-      $files[$file->fid] = $file;
+
+  if (!empty($fields)) {
+    foreach ($fields as $field) {
+      $db_info = content_database_info($field);
+      $result = db_query("SELECT f.* FROM {" . $db_info['table'] . "} c INNER JOIN {files} f ON c." . $db_info['columns']['fid']['column'] . " = f.fid WHERE f.filename LIKE '%s%%' AND status = 1", $filename);
+      while ($file = db_fetch_object($result)) {
+        $files[$file->fid] = $file;
+      }
     }
   }
 
Index: sources/remote.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/filefield_sources/sources/remote.inc,v
retrieving revision 1.2
diff -u -p -r1.2 remote.inc
--- sources/remote.inc	20 Sep 2009 20:57:20 -0000	1.2
+++ sources/remote.inc	3 Jan 2010 20:16:15 -0000
@@ -58,13 +58,14 @@ function filefield_source_remote_theme()
 /**
  * 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'));
+  }
 }
 
 /**
