You are here

function filefield_source_elfinder_value in elFinder file manager 7

Same name and namespace in other branches
  1. 8.2 modules/elfinder_ffsel/elfinder_ffsel.module \filefield_source_elfinder_value()
  2. 6.2 modules/elfinder_ffsel/elfinder_ffsel.module \filefield_source_elfinder_value()
  3. 6 modules/elfinder_ffsel/elfinder_ffsel.module \filefield_source_elfinder_value()
  4. 7.3 modules/elfinder_ffsel/elfinder_ffsel.module \filefield_source_elfinder_value()
  5. 7.2 modules/elfinder_ffsel/elfinder_ffsel.module \filefield_source_elfinder_value()

A #filefield_value_callback function.

1 string reference to 'filefield_source_elfinder_value'
filefield_source_elfinder_info in modules/elfinder_ffsel/elfinder_ffsel.module
Implementation of hook_filefield_source_info().

File

modules/elfinder_ffsel/elfinder_ffsel.module, line 147

Code

function filefield_source_elfinder_value($element, &$item) {
  if (isset($item['filefield_elfinder']['file_path']) && $item['filefield_elfinder']['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 = $wrapper
      ->getDirectoryPath();
    $uri = preg_replace('/^' . preg_quote('/' . $file_directory_prefix . '/', '/') . '/', $scheme . '://', $item['filefield_elfinder']['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 {
      if ($file = filefield_sources_save_file(elfinder_document_root() . $item['filefield_elfinder']['file_path'], $element['#upload_validators'], $element['#upload_location'])) {
        $item = array_merge($item, (array) $file);
        drupal_set_message(t('File created'));
      }
      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_elfinder']['file_path'] = '';
  }
}