You are here

function filefield_source_reference_value in FileField Sources 7

Same name and namespace in other branches
  1. 6 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 146
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 = file_load($fid)) {

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

        // Check that the user has access to this file through hook_download().
        if (!filefield_sources_file_access($file->uri)) {
          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, (array) $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'] = '';
  }
}