When I try to reference an existing file using Insert and this module, only a couple of results are shown by auto complete. If I type the entire filename of an existing file that is not seen by auto complete, it is not loaded. The form simply reloads. Auto complete should be seeing 10 files, but only shows two.

I've attached two screen shots to make this clear. The first is of auto complete showing only 2 results. The second is from the MySQL console showing 10 results for a like query.

Let me know what other information is needed.

Thanks and thanks for this awesome module.

Comments

solotandem’s picture

Title: Reference existing doesn't show all files » Reference existing doesn't show files associated with all content fields

I have the same problem. Before looking at the code, my expectation was that all files uploaded to any content field would be available for reuse with any other content field. After looking at the code, I see that you restrict the search to files previously associated with the content field being edited.

I also see that the documentation comment and function signature seem to anticipate searching among all content fields by default.

/**
 * Get all the files used within a particular field (or all fields).
 *
 * @param $file_name
 *   The partial name of the file to retrieve.
 * @param $field
 *   Optional. A CCK field array for which to filter returned files.
 */
function filefield_source_reference_get_files($filename, $field = NULL) {}

Yet the invocation of this function is always with a non-NULL field, thereby restricting the search to files associated with that content field. Is the "all fields search" an un-implemented feature? Would you unlock it by adding a widget setting? (If there is one and I have missed it, please let me know.)

solotandem’s picture

Status: Active » Needs review
StatusFileSize
new1.96 KB

The attached patch adds a widget setting and implements it.

asiby’s picture

This patch is incompatible with the module.

solotandem’s picture

What does "incompatible" mean? Based on what criteria? Please elaborate.

asiby’s picture

Thank you for your quick response. The patch cannot be applied. I had to add the changes manually. And it did not work. I am using the version 7.x-1.4 and I keep getting the error message "The file used in the Image field may not be referenced." when trying to reference an existing image.

I am familiar with the process of patching a file and this is not the first time for me to do it.

For example, if you look at this section of the file ....

--- a/sources/reference.inc
+++ b/sources/reference.inc
@@ -83,9 +83,16 @@ function filefield_source_reference_settings($op, $field) {
       '#type' => 'radios',
       '#default_value' => empty($field['filefield_source_autocomplete']) ? '0' : '1', 
     );
+    $return['sources_reference']['filefield_source_autocomplete_search_all_fields'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Search all fields'),
+      '#default_value' => empty($field['filefield_source_autocomplete_search_all_fields']) ? '0' : '1',
+      '#description' => t('If checked, the autocomplete search will include files previously associated with any content field. Otherwise, the search will be restricted to this content field.'),
+    );
   }
   elseif ($op == 'save') {
     $return[] = 'filefield_source_autocomplete';
+    $return[] = 'filefield_source_autocomplete_search_all_fields';
   }

The part that says '#default_value' => empty($field['filefield_source_autocomplete']) ? '0' : '1', does not exist in the current D7 version of the module.

I will appreciate any pointers for things that I need to adjust in order to make this work. It is very crucial because I have used the Media module and suddenly realized that the filefield_source will not read files uploaded using other fields. However, the Media module combined with the plupload module that I am using are not using any particular fields. Besides, the filefield is not yet supporting plupload.

Here is what I get when trying to patch the file

patching file reference.inc
Hunk #1 FAILED at 83.
Hunk #2 FAILED at 170.
2 out of 2 hunks FAILED -- saving rejects to file reference.inc.rej

Thanks again.

asiby’s picture

Sorry. I just realized that this patch was meant for the version 6.x-1.4

My mistake.

Any pointers about what I should change? I think it has to do with the file_usage table not being added populated during the referencing. Unfortunately, I am not yet familiar with that aspect of Drupal 7.

Cheers

asiby’s picture

I think I found it.

In file.module, there is this ...

function file_managed_file_validate(&$element, &$form_state) {
  // If referencing an existing file, only allow if there are existing
  // references. This prevents unmanaged files from being deleted if this
  // item were to be deleted.
  $clicked_button = end($form_state['triggering_element']['#parents']);
  if ($clicked_button != 'remove_button' && !empty($element['fid']['#value'])) {
    if ($file = file_load($element['fid']['#value'])) {
      if ($file->status == FILE_STATUS_PERMANENT) {
        $references = file_usage_list($file);
        if (empty($references)) {
          form_error($element, t('The file used in the !name field may not be referenced.', array('!name' => $element['#title'])));
        }
      }
    }
    else {
      form_error($element, t('The file referenced by the !name field does not exist.', array('!name' => $element['#title'])));
    }
  }
...

So I think that I might simply use a hook to replace this validation function that allows what I need to do.

imclean’s picture

giorgosk’s picture

Issue summary: View changes

for drupal 7 but perhaps this is also related #454982: Reference all filefield files