Newer
Older
/**
* @file
* FileField: Defines a CCK file field type.
*
* Uses content.module to store the fid and field specific metadata,
* and Drupal's {files} table to store the actual file data.
*
* This file contains CCK widget related functionality.
*/
* Implementation of CCK's hook_widget_settings($op == 'form').
*/
function filefield_widget_settings_form($widget) {
$form = array();
$form['file_extensions'] = array(
'#type' => 'textfield',
'#title' => t('Permitted upload file extensions'),
'#default_value' => is_string($widget['file_extensions']) ? $widget['file_extensions'] : 'txt',
'#size' => 64,
'#description' => t('Extensions a user can upload to this field. Separate extensions with a space and do not include the leading dot. Leaving this blank will allow users to upload a file with any extension.'),
'#weight' => 1,
);
$form['path_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Path settings'),
'#weight' => 6,
);
$form['path_settings']['file_path'] = array(
'#type' => 'textfield',
'#title' => t('File path'),
'#default_value' => is_string($widget['file_path']) ? $widget['file_path'] : '',
'#description' => t('Optional subdirectory within the "%directory" directory where files will be stored. Do not include preceding or trailing slashes.', array('%directory' => variable_get('file_directory_path', 'files') . '/')),
'#element_validate' => array('_filefield_widget_settings_file_path_validate'),
'#suffix' => theme('token_help', 'user'),
);
$form['max_filesize'] = array(
'#type' => 'fieldset',
'#title' => t('File size restrictions'),
'#description' => t('Limits for the size of files that a user can upload. Note that these settings only apply to newly uploaded files, whereas existing files are not affected.'),
'#weight' => 6,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['max_filesize']['max_filesize_per_file'] = array(
'#type' => 'textfield',
'#title' => t('Maximum upload size per file'),
'#default_value' => is_string($widget['max_filesize_per_file'])
? $widget['max_filesize_per_file']
: '',
Darrel O'Pry
committed
'#description' => t('Specify the size limit that applies to each file separately. Enter a value like "512" (bytes), "80K" (kilobytes) or "50M" (megabytes) in order to restrict the allowed file size. If you leave this this empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit <strong>%limit</strong>).', array('%limit' => format_size(file_upload_max_size()))),
'#element_validate' => array('_filefield_widget_settings_max_filesize_per_file_validate'),
);
$form['max_filesize']['max_filesize_per_node'] = array(
'#type' => 'textfield',
'#title' => t('Maximum upload size per node'),
'#default_value' => is_string($widget['max_filesize_per_node'])
? $widget['max_filesize_per_node']
: '',
'#description' => t('Specify the total size limit for all files in field on a given node. Enter a value like "512" (bytes), "80K" (kilobytes) or "50M" (megabytes) in order to restrict the total size of a node. Leave this empty if there should be no size restriction.'),
'#element_validate' => array('_filefield_widget_settings_max_filesize_per_node_validate'),
);
return $form;
}
/**
* Implementation of CCK's hook_widget_settings($op == 'save').
*/
function filefield_widget_settings_save($widget) {
return array(
'file_extensions', 'file_path', 'max_filesize_per_file',
'max_filesize_per_node', 'file_widgets'
);
}
function _filefield_widget_settings_file_path_validate($element, &$form_state) {
// Strip slashes from the beginning and end of $widget['file_path']
$form_state['values']['file_path'] = trim($form_state['values']['file_path'], '\\/');
}
function _filefield_widget_settings_max_filesize_per_file_validate($element, &$form_state) {
if (empty($form_state['values']['max_filesize_per_file'])) {
return; // Empty means no size restrictions, so don't throw an error.
}
elseif (!is_numeric(parse_size($form_state['values']['max_filesize_per_file']))) {
form_error($element, t('The "@field" option must contain a valid value. You can either leave the text field empty or enter a string like "512" (bytes), "80K" (kilobytes) or "50M" (megabytes).', array('@field' => t('Maximum upload size per file'))));
}
}
function _filefield_widget_settings_max_filesize_per_node_validate($element, &$form_state) {
if (empty($form_state['values']['max_filesize_per_node'])) {
return; // Empty means no size restrictions, so don't throw an error.
}
elseif (!is_numeric(parse_size($form_state['values']['max_filesize_per_node']))) {
form_error($element, t('The "@field" option must contain a valid value. You can either leave the text field empty or enter a string like "512" (bytes), "80K" (kilobytes) or "50M" (megabytes).', array('@field' => t('Maximum upload size per node'))));
andrew morton
committed
/**
* Determine the widget's files directory
*
* @param $field
* A CCK field array.
* @return
* The files directory path, with any tokens replaced.
andrew morton
committed
*/
function filefield_widget_file_path($field_instance) {
$dest = $field_instance['widget']['file_path'];
if (module_exists('token')) {
global $user;
$dest = token_replace($dest, 'user', $user);
}
return file_directory_path() .'/'. $dest;
}
/**
* Given a FAPI element, save any files that may have been uploaded into it.
*
* This function should only be called during validate, submit, or
* value_callback functions.
*
* @param $element
* The FAPI element whose values are being saved.
*/
Darrel O'Pry
committed
function filefield_save_upload($element) {
$upload_name = $element['#field_name'] .'_'. $element['#delta'];
$field_instance = content_fields($element['#field_name'], $element['#type_name']);
if (empty($_FILES['files']['name'][$upload_name])) {
return 0;
}
andrew morton
committed
$dest = filefield_widget_file_path($field_instance);
Darrel O'Pry
committed
if (!field_file_check_directory($dest, FILE_CREATE_DIRECTORY)) {
watchdog('filefield', 'The upload directory %directory for the file field %field (content type %type) 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' => $dest, '%field' => $element['#field_name'], '%type' => $element['#type_name']));
Darrel O'Pry
committed
form_set_error($upload_name, t('The file could not be uploaded.'));
return 0;
}
if (!$file = field_file_save_upload($upload_name, $element['#upload_validators'], $dest)) {
watchdog('filefield', 'The file upload failed. %upload', array('%upload' => $upload_name));
form_set_error($upload_name, t('The file in the @field field was unable to be uploaded.', array('@field' => $element['#title'])));
Darrel O'Pry
committed
return 0;
}
return $file['fid'];
}
* The #value_callback for the filefield_widget type element.
Darrel O'Pry
committed
function filefield_widget_value($element, $edit = FALSE) {
$file = field_file_load($element['#default_value']['fid']);
Darrel O'Pry
committed
$item = $element['#default_value'];
}
else {
$item = array_merge($element['#default_value'], $edit);
$field = content_fields($element['#field_name'], $element['#type_name']);
// Uploads take priority over value of fid text field.
Darrel O'Pry
committed
if ($fid = filefield_save_upload($element)) {
$item['fid'] = $fid;
// Load file if the FID has changed so that it can be saved by CCK.
$file = field_file_load($item['fid']);
// Checkboxes loose their value when empty.
// If the list field is present make sure its unchecked value is saved.
if ($field['list_field'] && empty($edit['list'])) {
$item['list'] = 0;
}
Darrel O'Pry
committed
}
// Merge file and item data so it is available to all widgets.
Darrel O'Pry
committed
}
/**
* An element #process callback for the filefield_widget field type.
*
* Expands the filefield_widget type to include the upload field, upload and
* remove buttons, and the description field.
*/
Darrel O'Pry
committed
function filefield_widget_process($element, $edit, &$form_state, $form) {
// The widget is being presented, so apply the JavaScript.
drupal_add_js(drupal_get_path('module', 'filefield') .'/filefield.js');
Darrel O'Pry
committed
$item = $element['#value'];
$field_name = $element['#field_name'];
$delta = $element['#delta'];
Nate Lampton
committed
$element['#theme'] = 'filefield_widget_item';
Darrel O'Pry
committed
$field = content_fields($element['#field_name'], $element['#type_name']);
Nate Lampton
committed
// Title is not necessary for each individual field.
if ($field['multiple'] > 0) {
unset($element['#title']);
}
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Set up the buttons first since we need to check if they were clicked.
$element['filefield_upload'] = array(
'#type' => 'submit',
'#value' => t('Upload'),
'#process' => array('form_expand_ahah'),
'#submit' => array('node_form_submit_build_node'),
'#ahah' => array( // with JavaScript
'path' => 'filefield/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'],
'wrapper' => $element['#id'] .'-ahah-wrapper',
'method' => 'replace',
'effect' => 'fade',
),
'#field_name' => $element['#field_name'],
'#delta' => $element['#delta'],
'#type_name' => $element['#type_name'],
'#upload_validators' => $element['#upload_validators'],
'#weight' => 100,
'#post' => $element['#post'],
);
$element['filefield_remove'] = array(
'#name' => $element['#field_name'] .'_'. $element['#delta'] .'_filefield_remove',
'#type' => 'submit',
'#value' => t('Remove'),
'#process' => array('form_expand_ahah'),
'#submit' => array('node_form_submit_build_node'),
'#ahah' => array( // with JavaScript
'path' => 'filefield/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'],
'wrapper' => $element['#id'] .'-ahah-wrapper',
'method' => 'replace',
'effect' => 'fade',
),
'#field_name' => $element['#field_name'],
'#delta' => $element['#delta'],
'#weight' => 101,
'#post' => $element['#post'],
);
// Because the output of this field changes depending on the button clicked,
// we need to ask FAPI immediately if the remove button was clicked.
// It's not good that we call this private function, but
// $form_state['clicked_button'] is only available after this #process
// callback is finished.
if (_form_button_was_clicked($element['filefield_remove'])) {
$item = array('fid' => 0, 'list' => $field['list_default'], 'data' => array('description' => ''));
Darrel O'Pry
committed
}
// Set access on the buttons.
$element['filefield_upload']['#access'] = empty($item['fid']);
$element['filefield_remove']['#access'] = !empty($item['fid']);
// Figure out our fid...
$element['fid'] = array('#type' => 'hidden', '#value' => $item['fid']);
Darrel O'Pry
committed
$element['preview'] = array(
'#type' => 'markup',
'#value' => theme('filefield_widget_preview', $item),
);
Darrel O'Pry
committed
}
Darrel O'Pry
committed
// placeholder.. will be serialized into the data column. this is a place for widgets
// to put additional data.
$element['data'] = array(
'#tree' => 'true',
'#access' => !empty($item['fid']),
);
andrew morton
committed
if ($field['description_field']) {
andrew morton
committed
$element['data']['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#value' => isset($item['data']['description']) ? $item['data']['description'] : '',
);
}
Darrel O'Pry
committed
if ($field['list_field']) {
Darrel O'Pry
committed
$element['list'] = array(
'#type' => 'checkbox',
'#title' => t('List'),
Darrel O'Pry
committed
'#value' => isset($item['list']) ? $item['list'] : $field['list_default'],
Darrel O'Pry
committed
'#attributes' => array('class' => 'filefield-list'),
Nate Lampton
committed
'#access' => !empty($item['fid']),
Darrel O'Pry
committed
);
}
else {
$element['list'] = array(
'#type' => 'hidden',
'#value' => '1',
);
}
Darrel O'Pry
committed
Darrel O'Pry
committed
foreach ($element['#upload_validators'] as $callback => $arguments) {
$help_func = $callback .'_help';
Darrel O'Pry
committed
if (function_exists($help_func)) {
$desc[] = call_user_func_array($help_func, $arguments);
}
Darrel O'Pry
committed
}
$element['upload'] = array(
'#name' => 'files['. $element['#field_name'] .'_'. $element['#delta'] .']',
'#type' => 'file',
'#description' => implode('<br />', $desc),
Nate Lampton
committed
'#size' => 22,
Darrel O'Pry
committed
'#attributes' => array(
Darrel O'Pry
committed
'accept' => implode(',', array_filter(explode(' ', $field['widget']['file_extensions']))),
Nate Lampton
committed
),
'#access' => empty($item['fid']),
Darrel O'Pry
committed
);
$element['#attributes']['id'] = $element['#id'] .'-ahah-wrapper';
$element['#prefix'] = '<div '. drupal_attributes($element['#attributes']) .'>';
Darrel O'Pry
committed
$element['#suffix'] = '</div>';
Darrel O'Pry
committed
return $element;
}
/**
* An #element_validate callback for the filefield_widget field.
*/
Darrel O'Pry
committed
function filefield_widget_validate($element, &$form_state) {
// Currently all handled by filefield_widget_upload_validators().
Darrel O'Pry
committed
}
/**
* FormAPI theme function. Theme the output of an image field.
*/
function theme_filefield_widget($element) {
return theme('form_element', $element, $element['#children']);
Darrel O'Pry
committed
}
function theme_filefield_widget_preview($item) {
// Remove the current description so that we get the filename as the link.
if (isset($item['data']['description'])) {
unset($item['data']['description']);
}
return '<div class="filefield-file-info">'.
'<div class="filename">'. theme('filefield_file', $item) .'</div>'.
'<div class="filesize">'. format_size($item['filesize']) .'</div>'.
'<div class="filemime">'. $item['filemime'] .'</div>'.
'</div>';
}
function theme_filefield_widget_item($element) {
Nate Lampton
committed
// Put the upload button directly after the upload field.
$element['upload']['#field_suffix'] = drupal_render($element['filefield_upload']);
Nate Lampton
committed
$element['upload']['#theme'] = 'filefield_widget_file';
$output = '';
$output .= '<div class="filefield-element clear-block">';
if ($element['fid']['#value'] != 0) {
$output .= '<div class="widget-preview">';
Nate Lampton
committed
$output .= drupal_render($element['preview']);
$output .= '</div>';
}
$output .= '<div class="widget-edit">';
Nate Lampton
committed
$output .= drupal_render($element);
$output .= '</div>';
$output .= '</div>';
return $output;
}
/**
* Custom theme function for FileField upload elements.
*
* This function allows us to put the "Upload" button immediately after the
* file upload field by respecting the #field_suffix property.
Nate Lampton
committed
*/
function theme_filefield_widget_file($element) {
$output = '';
$output .= '<div class="filefield-upload clear-block">';
if (isset($element['#field_prefix'])) {
$output .= $element['#field_prefix'];
}
_form_set_class($element, array('form-file'));
$output .= '<input type="file" name="'. $element['#name'] .'"'. ($element['#attributes'] ? ' '. drupal_attributes($element['#attributes']) : '') .' id="'. $element['#id'] .'" size="'. $element['#size'] ."\" />\n";
if (isset($element['#field_suffix'])) {
$output .= $element['#field_suffix'];
}
$output .= '</div>';
return theme('form_element', $element, $output);
Darrel O'Pry
committed
Darrel O'Pry
committed
/**
* Additional #validate handler for the node form.
*
* This function checks the #required properties on file fields and calculates
* node upload totals for all file fields. The #required property is not
* properly supported on file fields by Drupal core, so we do this manually.
Darrel O'Pry
committed
*/
function filefield_node_form_validate($form, &$form_state) {
$type = content_types($form['type']['#value']);
foreach ($type['fields'] as $field_name => $field) {
Darrel O'Pry
committed
if (!(in_array($field['module'], array('imagefield', 'filefield')))) continue;
Darrel O'Pry
committed
$empty = $field['module'] .'_content_is_empty';
$total_filesize = 0;
Nate Lampton
committed
if (!empty($form_state['values'][$field_name])) {
foreach ($form_state['values'][$field_name] as $delta => $item) {
Nate Lampton
committed
if ($empty($item, $field)) continue;
$valid = TRUE;
$total_filesize += (int)$item['filesize'];
}
Darrel O'Pry
committed
}
Darrel O'Pry
committed
if (!$valid && $field['required']) {
form_set_error($field_name, t('%title field is required.', array('%title' => $field['widget']['label'])));
}
$max_filesize = parse_size($field['widget']['max_filesize_per_node']);
if ($max_filesize && $total_filesize > $max_filesize) {
form_set_error($field_name, t('Total filesize for %title, %tsize, exceeds field settings of %msize.',
'%title' => $field['widget']['label'],
'%tsize' => format_size($total_filesize),
'%msize' => format_size($max_filesize)
)
));
}
Darrel O'Pry
committed
}
}
/**
* Additional #submit handler for the node form.
*/
function filefield_node_form_submit($form, &$form_state) {
// Only add additional submit handling on save.
if ($form_state['values']['op'] == t('Save')) {
// TODO: Immediately delete temporary files that were uploaded but never
// saved. This isn't critical to do, since any file not saved with the node
// will by cleaned up by system_cron() after 6 hours.