function filefield_sources_plupload_element_value in FileField Sources Plupload 7
Our value handler not only sets the Plupload fields values, but also adds the files to the filefiels values.
1 string reference to 'filefield_sources_plupload_element_value'
- filefield_sources_plupload_source_process in ./
filefield_sources_plupload.module - A #process callback to extend the filefield_widget element type.
File
- ./
filefield_sources_plupload.module, line 103 - A File field extension to allow multi uploads using Plupload.
Code
function filefield_sources_plupload_element_value($element, $input = FALSE, &$form_state = NULL) {
if (isset($input) && $input === FALSE) {
return array();
}
// We rely on Plupload Integration module to handle the actual field values.
$pud_value = plupload_element_value($element, $input, $form_state);
if (empty($pud_value)) {
return array();
}
$field_parents = array_slice($element['#array_parents'], 0, -3);
$field_element = drupal_array_get_nested_value($form_state['complete form'], $field_parents);
$field_name = $field_element['#field_name'];
$langcode = $field_element['#language'];
$upload_delta = isset($field_element['#file_upload_delta']) ? $field_element['#file_upload_delta'] : 0;
$upload_location = isset($field_element[$upload_delta]['#upload_location']) ? $field_element[$upload_delta]['#upload_location'] : file_default_scheme() . '://';
$upload_validators = isset($field_element[$upload_delta]['#upload_validators']) ? $field_element[$upload_delta]['#upload_validators'] : array();
// A URI may already have a trailing slash or look like "public://".
if (drupal_substr($upload_location, -1) != '/') {
$upload_location .= '/';
}
if (!file_prepare_directory($upload_location, FILE_CREATE_DIRECTORY)) {
watchdog('file', 'The upload directory %directory for the file field !name could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', array(
'%directory' => $upload_location,
'!name' => $field_name,
));
form_set_error($field_name, t('The file could not be uploaded.'));
return FALSE;
}
// Validate, clean up and move the files into it's destination, then register
// as managed files (status = 0 until entity is saved).
$saved_files = array();
foreach ($pud_value as $uploaded_file) {
if ($uploaded_file['status'] == 'done') {
$source = $uploaded_file['tmppath'];
$extensions = $upload_validators['file_validate_extensions'][0];
// Transliterate, munge and validate file name.
$filename = filefield_sources_clean_filename($uploaded_file['name'], $extensions);
// Move the file to a temporary destination using final base file name.
$temp_destination = file_stream_wrapper_uri_normalize('temporary://' . $filename);
$temp_filepath = file_unmanaged_move($source, $temp_destination, FILE_EXISTS_REPLACE);
// Save the files to their final destination.
if ($file = filefield_sources_save_file($temp_filepath, $upload_validators, $upload_location)) {
file_unmanaged_delete($temp_filepath);
$saved_files[] = $file;
}
}
else {
form_set_error('pud', t('Upload of %name failed.', array(
'%name' => $uploaded_file['name'],
)));
}
}
// Get exisitng file values.
// File Field items are stored in the field state starting from Drupal 7.9.
$field_state = field_form_get_state($field_element['#field_parents'], $field_name, $langcode, $form_state);
if (isset($field_state['items'])) {
$field_values = $field_state['items'];
}
else {
$field_values = drupal_array_get_nested_value($form_state['values'], $field_parents);
}
// Update field values with new files.
foreach ($saved_files as $saved_file) {
$field_values[$upload_delta] = (array) $saved_file;
$field_values[$upload_delta]['_weight'] = $upload_delta;
$upload_delta++;
}
// Update form_state values.
drupal_array_set_nested_value($form_state['values'], $field_parents, $field_values);
// Update items.
$field_state['items'] = $field_values;
field_form_set_state($field_element['#field_parents'], $field_name, $langcode, $form_state, $field_state);
return $pud_value;
}