function filefield_source_attach_value in FileField Sources 7
Same name and namespace in other branches
- 6 sources/attach.inc \filefield_source_attach_value()
A #filefield_value_callback function.
1 string reference to 'filefield_source_attach_value'
- filefield_source_attach_info in sources/
attach.inc - Implements hook_filefield_source_info().
File
- sources/
attach.inc, line 221 - A FileField extension to allow use of files within a server directory.
Code
function filefield_source_attach_value($element, &$item) {
if (!empty($item['filefield_attach']['filename'])) {
$instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
$filepath = $item['filefield_attach']['filename'];
// Check that the destination is writable.
$directory = $element['#upload_location'];
$mode = variable_get('file_chmod_directory', 0775);
// This first chmod check is for other systems such as S3, which don't work
// with file_prepare_directory().
if (!drupal_chmod($directory, $mode) && !file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
watchdog('file', 'File %file could not be copied, because the destination directory %destination is not configured correctly.', array(
'%file' => $filepath,
'%destination' => drupal_realpath($directory),
));
drupal_set_message(t('The specified file %file could not be copied, because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', array(
'%file' => $filepath,
)), 'error');
return;
}
// Clean up the file name extensions and transliterate.
$original_filepath = $filepath;
$new_filepath = filefield_sources_clean_filename($filepath, $instance['settings']['file_extensions']);
rename($filepath, $new_filepath);
$filepath = $new_filepath;
// Run all the normal validations, minus file size restrictions.
$validators = $element['#upload_validators'];
if (isset($validators['file_validate_size'])) {
unset($validators['file_validate_size']);
}
// Save the file to the new location.
if ($file = filefield_sources_save_file($filepath, $validators, $directory)) {
$item = array_merge($item, (array) $file);
// Delete the original file if "moving" the file instead of copying.
if (drupal_realpath($filepath) !== drupal_realpath($file->uri) && $instance['widget']['settings']['filefield_sources']['source_attach']['attach_mode'] !== 'copy') {
@unlink($filepath);
}
}
// Restore the original file name if the file still exists.
if (file_exists($filepath) && $filepath != $original_filepath) {
rename($filepath, $original_filepath);
}
$item['filefield_attach']['filename'] = '';
}
}