You are here

function filefield_source_imce_value in FileField Sources 7

Same name and namespace in other branches
  1. 6 sources/imce.inc \filefield_source_imce_value()

A #filefield_value_callback function.

1 string reference to 'filefield_source_imce_value'
filefield_source_imce_info in sources/imce.inc
Implements hook_filefield_source_info().

File

sources/imce.inc, line 155
A FileField extension to allow referencing of files from IMCE.

Code

function filefield_source_imce_value($element, &$item) {
  if (isset($item['filefield_imce']['file_path']) && $item['filefield_imce']['file_path'] != '') {
    $field = field_info_field($element['#field_name']);
    $scheme = $field['settings']['uri_scheme'];
    $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
    $file_directory_prefix = $scheme == 'private' ? 'system/files' : $wrapper
      ->getDirectoryPath();
    $uri = preg_replace('/^' . preg_quote(base_path() . $file_directory_prefix . '/', '/') . '/', $scheme . '://', $item['filefield_imce']['file_path']);

    // Resolve the file path to an FID.
    $fid = db_select('file_managed', 'f')
      ->condition('uri', rawurldecode($uri))
      ->fields('f', array(
      'fid',
    ))
      ->execute()
      ->fetchField();
    if ($fid) {
      $file = file_load($fid);
      if (filefield_sources_element_validate($element, $file)) {
        $item = array_merge($item, (array) $file);
      }
    }
    else {
      form_error($element, t('The selected file could not be used because the file does not exist in the database.'));
    }

    // No matter what happens, clear the value from the file path field.
    $item['filefield_imce']['file_path'] = '';
  }
}