function s3fs_cors_upload_process in S3 File System CORS Upload 7
Element process function for s3fs_cors_upload element.
Expands the element to include Upload and Remove buttons, as well as support for a default value.
In order to take advantage of the work that file.module is already doing for elements of type #managed_file, we stick to the same naming convention here.
1 string reference to 's3fs_cors_upload_process'
- s3fs_cors_element_info in ./
s3fs_cors.module - Implements hook_element_info().
File
- ./
s3fs_cors.module, line 214 - Allow uploading of files directly to AmazonS3 via the browser using CORS.
Code
function s3fs_cors_upload_process($element, &$form_state, &$form) {
$fid = isset($element['#value']['fid']) ? $element['#value']['fid'] : 0;
$element['#file'] = $fid ? file_load($fid) : FALSE;
$element['#tree'] = TRUE;
$parents_id = implode('_', $element['#parents']);
// AJAX settings used for upload and remove buttons.
$ajax_settings = array(
'callback' => 's3fs_cors_upload_js',
'wrapper' => "{$element['#id']}-ajax-wrapper",
'method' => 'replace',
'effect' => 'fade',
);
// The "Upload" button.
$element['upload_button'] = array(
'#name' => "{$parents_id}_upload_button",
'#type' => 'submit',
'#value' => t('Upload'),
'#validate' => array(),
'#limit_validation_errors' => array(
$element['#parents'],
),
'#attributes' => array(
'class' => array(
'cors-form-submit',
),
),
'#weight' => -5,
'#submit' => array(
's3fs_cors_upload_submit',
),
'#ajax' => $ajax_settings,
);
// The "Remove" button.
$element['remove_button'] = array(
'#name' => "{$parents_id}_remove_button",
'#type' => 'submit',
'#value' => t('Remove'),
'#validate' => array(),
'#limit_validation_errors' => array(
$element['#parents'],
),
'#attributes' => array(
'class' => array(
'cors-form-remove',
),
),
'#weight' => -5,
'#submit' => array(
's3fs_cors_remove_submit',
),
'#ajax' => $ajax_settings,
);
// The file upload field itself.
$element['upload'] = array(
'#name' => "files[{$parents_id}]",
'#type' => 'file',
'#title' => t('Choose a file.'),
'#title_display' => 'invisible',
'#size' => $element['#size'],
'#theme_wrappers' => array(),
'#weight' => -10,
'#attributes' => array(
'class' => array(
's3fs-cors-upload-file',
),
'multiple' => 'multiple',
),
);
if ($fid && $element['#file']) {
$element['filelink'] = array(
'#type' => 'markup',
'#markup' => theme('file_link', array(
'file' => $element['#file'],
)) . ' ',
'#weight' => -10,
);
}
// Add the extension list to the page as JavaScript settings.
if (isset($element['#upload_validators']['file_validate_extensions'][0])) {
$extension_list = implode(',', array_filter(explode(' ', $element['#upload_validators']['file_validate_extensions'][0])));
$element['upload']['#attached']['js'] = array(
array(
'type' => 'setting',
'data' => array(
'file' => array(
'elements' => array(
"#{$element['#id']}-upload" => $extension_list,
),
'max_upload_size' => array(
$element['upload']['#name'] => $element['#upload_validators']['file_validate_size'][0],
),
),
),
),
);
}
// These hidden elements get populated by javascript after uploading the file
// to S3. They are then used by the value callback to save the new file record
// to the DB.
$element['fid'] = array(
'#type' => 'hidden',
'#value' => $fid,
'#attributes' => array(
'class' => array(
'fid',
),
),
);
$element['filename'] = array(
'#type' => 'hidden',
'#default_value' => isset($element['#file']->filename) ? $element['#file']->filename : '',
'#attributes' => array(
'class' => array(
'filename',
),
),
// This keeps theme_file_widget() happy.
'#markup' => '',
);
$element['filemime'] = array(
'#type' => 'hidden',
'#attributes' => array(
'class' => array(
'filemime',
),
),
'#default_value' => isset($element['#file']->filemime) ? $element['#file']->filemime : '',
);
$element['filesize'] = array(
'#type' => 'hidden',
'#attributes' => array(
'class' => array(
'filesize',
),
),
'#default_value' => isset($element['#file']->filesize) ? $element['#file']->filesize : '',
);
// Add a class to the <form> element so we can find it with JS later.
$form['#attributes']['class'][] = 's3fs-cors-upload-form';
$element['#prefix'] = "<div id=\"{$element['#id']}-ajax-wrapper\">";
$element['#suffix'] = '</div>';
return $element;
}