You are here

function filefield_source_reference_value in FileField Sources 6

Same name and namespace in other branches
  1. 7 sources/reference.inc \filefield_source_reference_value()

A #filefield_value_callback function.

1 string reference to 'filefield_source_reference_value'
filefield_source_reference_info in sources/reference.inc
Implements hook_filefield_source_info().

File

sources/reference.inc, line 133
A FileField extension to allow referencing of existing files.

Code

function filefield_source_reference_value($element, &$item) {
  if (isset($item['filefield_reference']['autocomplete']) && strlen($item['filefield_reference']['autocomplete']) > 0 && $item['filefield_reference']['autocomplete'] != FILEFIELD_SOURCE_REFERENCE_HINT_TEXT) {
    $matches = array();
    if (preg_match('/\\[fid:(\\d+)\\]/', $item['filefield_reference']['autocomplete'], $matches)) {
      $fid = $matches[1];
      if ($file = field_file_load($fid)) {

        // Remove file size restrictions, since the file already exists on disk.
        if (isset($element['#upload_validators']['filefield_validate_size'])) {
          unset($element['#upload_validators']['filefield_validate_size']);
        }

        // Check that the user has access to this file through hook_download().
        if (!filefield_sources_file_access($file['filepath'])) {
          form_error($element, t('You do not have permission to use the selected file.'));
        }
        elseif (filefield_sources_element_validate($element, (object) $file)) {
          $item = array_merge($item, $file);
        }
      }
      else {
        form_error($element, t('The referenced file could not be used because the file does not exist in the database.'));
      }
    }

    // No matter what happens, clear the value from the autocomplete.
    $item['filefield_reference']['autocomplete'] = '';
  }
}