You are here

function filefield_source_elfinder_value in elFinder file manager 6

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. 7.3 modules/elfinder_ffsel/elfinder_ffsel.module \filefield_source_elfinder_value()
  4. 7 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 145

Code

function filefield_source_elfinder_value($element, &$item) {
  $field = content_fields($element['#field_name'], $element['#type_name']);
  $validators = $element['#upload_validators'];
  if (isset($item['filefield_elfinder']['file_path']) && $item['filefield_elfinder']['file_path'] != '') {
    if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) {
      $file_path = preg_replace('/^' . preg_quote(base_path(), '/') . '/', '', $item['filefield_elfinder']['file_path']);
    }
    else {
      $file_path = preg_replace('/^' . preg_quote(url('system/files'), '/') . '/', file_directory_path(), $item['filefield_elfinder']['file_path']);
    }

    // Resolve the file path to an FID.
    if ($fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", rawurldecode($file_path)))) {
      if ($file = field_file_load($fid)) {

        // Run all the normal validations, minus file size restrictions.
        if (isset($element['#upload_validators']['filefield_validate_size'])) {
          unset($element['#upload_validators']['filefield_validate_size']);
        }
        if (filefield_sources_element_validate($element, (object) $file)) {
          $item = array_merge($item, $file);
        }
      }
      else {
        form_error($element, t('The referenced file could not be used'));
      }
    }
    else {

      // We create a new file from original that is referenced to the node.
      // NOTE: New file is saved at the fielfield path, set in field settings for content type
      if ($file = field_file_save_file(rawurldecode($file_path), $validators, filefield_widget_file_path($field))) {
        $item = array_merge($item, $file);
        drupal_set_message(t('File created'));
      }

      // Note: we do not remove original file - so it can be used in other places.
    }

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