summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2013-04-20 03:34:14 (GMT)
committerwebchick2013-04-20 03:34:14 (GMT)
commit8fd2b4779ec83c3e44a8caac65300b96e3a52ab8 (patch)
treee59373e2bff86428ececfca3383ccf8fe481677f
parent8733925ff7f7f8f2e73f95020d37a3cf113127c0 (diff)
Issue #625958 by slashrsm, quicksketch, eojthebrave, droplet, danlinn, czigor: Support Uploading Multiple Files for HTML5 Browsers via #multiple attribute.
-rw-r--r--core/includes/file.inc314
-rw-r--r--core/includes/form.inc11
-rw-r--r--core/modules/aggregator/aggregator.admin.inc2
-rw-r--r--core/modules/file/file.field.inc91
-rw-r--r--core/modules/file/file.install31
-rw-r--r--core/modules/file/file.module199
-rw-r--r--core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php40
-rw-r--r--core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php7
-rw-r--r--core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php2
-rw-r--r--core/modules/file/lib/Drupal/file/Tests/FileManagedFileElementTest.php170
-rw-r--r--core/modules/file/tests/file_module_test.module24
-rw-r--r--core/modules/file/tests/file_test/file_test.module2
-rw-r--r--core/modules/image/image.field.inc26
-rw-r--r--core/modules/image/image.module25
-rw-r--r--core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php6
-rw-r--r--core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php24
-rw-r--r--core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php6
-rw-r--r--core/modules/locale/locale.bulk.inc2
-rw-r--r--core/modules/node/node.api.php2
-rw-r--r--core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php4
-rw-r--r--core/modules/system/system.admin.inc4
-rw-r--r--core/modules/system/system.module2
-rw-r--r--core/modules/update/update.manager.inc2
23 files changed, 645 insertions, 351 deletions
diff --git a/core/includes/file.inc b/core/includes/file.inc
index ea41955..e3ac8ea 100644
--- a/core/includes/file.inc
+++ b/core/includes/file.inc
@@ -1020,14 +1020,14 @@ function file_unmanaged_delete_recursive($path, $callback = NULL) {
}
/**
- * Saves a file upload to a new location.
+ * Saves file uploads to a new location.
*
- * The file will be added to the {file_managed} table as a temporary file.
+ * The files will be added to the {file_managed} table as temporary files.
* Temporary files are periodically cleaned. Use file_usage()->add() to register
* the usage of the file which will automatically mark it as permanent.
*
* @param $source
- * A string specifying the filepath or URI of the uploaded file to save.
+ * A string specifying the filepath or URI of the uploaded files to save.
* @param $validators
* An optional, associative array of callback functions used to validate the
* file. See file_validate() for a full discussion of the array format.
@@ -1041,6 +1041,8 @@ function file_unmanaged_delete_recursive($path, $callback = NULL) {
* A string containing the URI $source should be copied to.
* This must be a stream wrapper URI. If this value is omitted, Drupal's
* temporary files scheme will be used ("temporary://").
+ * @param $delta
+ * Delta of the file to save or NULL to save all files. Defaults to NULL.
* @param $replace
* Replace behavior when the destination file already exists:
* - FILE_EXISTS_REPLACE: Replace the existing file.
@@ -1049,181 +1051,209 @@ function file_unmanaged_delete_recursive($path, $callback = NULL) {
* - FILE_EXISTS_ERROR: Do nothing and return FALSE.
*
* @return
- * An object containing the file information if the upload succeeded, FALSE
- * in the event of an error, or NULL if no file was uploaded. The
- * documentation for the "File interface" group, which you can find under
+ * Function returns array of files or a single file object if $delta
+ * != NULL. Each file object contains the file information if the
+ * upload succeeded or FALSE in the event of an error. Function
+ * returns NULL if no file was uploaded.
+ *
+ * The documentation for the "File interface" group, which you can find under
* Related topics, or the header at the top of this file, documents the
* components of a file entity. In addition to the standard components,
* this function adds:
* - source: Path to the file before it is moved.
* - destination: Path to the file after it is moved (same as 'uri').
*/
-function file_save_upload($source, $validators = array(), $destination = FALSE, $replace = FILE_EXISTS_RENAME) {
+function file_save_upload($source, $validators = array(), $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME) {
global $user;
static $upload_cache;
+ // Make sure there's an upload to process.
+ if (empty($_FILES['files']['name'][$source])) {
+ return NULL;
+ }
+
// Return cached objects without processing since the file will have
- // already been processed and the paths in _FILES will be invalid.
+ // already been processed and the paths in $_FILES will be invalid.
if (isset($upload_cache[$source])) {
+ if (isset($delta)) {
+ return $upload_cache[$source][$delta];
+ }
return $upload_cache[$source];
}
- // Make sure there's an upload to process.
- if (empty($_FILES['files']['name'][$source])) {
- return NULL;
+ // Prepare uploaded files info. Representation is slightly different
+ // for multiple uploads and we fix that here.
+ $uploaded_files = $_FILES;
+ if (!is_array($uploaded_files['files']['name'][$source])) {
+ foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $value)
+ $uploaded_files['files'][$value][$source] = array($uploaded_files['files'][$value][$source]);
}
- // Check for file upload errors and return FALSE if a lower level system
- // error occurred. For a complete list of errors:
- // See http://php.net/manual/features.file-upload.errors.php.
- switch ($_FILES['files']['error'][$source]) {
- case UPLOAD_ERR_INI_SIZE:
- case UPLOAD_ERR_FORM_SIZE:
- drupal_set_message(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', array('%file' => $_FILES['files']['name'][$source], '%maxsize' => format_size(file_upload_max_size()))), 'error');
- return FALSE;
+ $files = array();
+ foreach ($uploaded_files['files']['name'][$source] as $i => $name) {
+ // Check for file upload errors and return FALSE for this file if a lower
+ // level system error occurred. For a complete list of errors:
+ // See http://php.net/manual/features.file-upload.errors.php.
+ switch ($uploaded_files['files']['error'][$source][$i]) {
+ case UPLOAD_ERR_INI_SIZE:
+ case UPLOAD_ERR_FORM_SIZE:
+ drupal_set_message(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', array('%file' => $name, '%maxsize' => format_size(file_upload_max_size()))), 'error');
+ $files[$i] = FALSE;
+ continue;
- case UPLOAD_ERR_PARTIAL:
- case UPLOAD_ERR_NO_FILE:
- drupal_set_message(t('The file %file could not be saved because the upload did not complete.', array('%file' => $_FILES['files']['name'][$source])), 'error');
- return FALSE;
+ case UPLOAD_ERR_PARTIAL:
+ case UPLOAD_ERR_NO_FILE:
+ drupal_set_message(t('The file %file could not be saved because the upload did not complete.', array('%file' => $name)), 'error');
+ $files[$i] = FALSE;
+ continue;
- case UPLOAD_ERR_OK:
- // Final check that this is a valid upload, if it isn't, use the
- // default error handler.
- if (is_uploaded_file($_FILES['files']['tmp_name'][$source])) {
- break;
- }
+ case UPLOAD_ERR_OK:
+ // Final check that this is a valid upload, if it isn't, use the
+ // default error handler.
+ if (is_uploaded_file($uploaded_files['files']['tmp_name'][$source][$i])) {
+ break;
+ }
+
+ // Unknown error
+ default:
+ drupal_set_message(t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $name)), 'error');
+ $files[$i] = FALSE;
+ continue;
- // Unknown error
- default:
- drupal_set_message(t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $_FILES['files']['name'][$source])), 'error');
- return FALSE;
- }
- // Begin building file entity.
- $values = array(
- 'uid' => $user->uid,
- 'status' => 0,
- 'filename' => trim(drupal_basename($_FILES['files']['name'][$source]), '.'),
- 'uri' => $_FILES['files']['tmp_name'][$source],
- 'filesize' => $_FILES['files']['size'][$source],
- );
- $values['filemime'] = file_get_mimetype($values['filename']);
- $file = entity_create('file', $values);
-
- $extensions = '';
- if (isset($validators['file_validate_extensions'])) {
- if (isset($validators['file_validate_extensions'][0])) {
- // Build the list of non-munged extensions if the caller provided them.
- $extensions = $validators['file_validate_extensions'][0];
+ }
+ // Begin building file entity.
+ $values = array(
+ 'uid' => $user->uid,
+ 'status' => 0,
+ 'filename' => trim(drupal_basename($name, '.')),
+ 'uri' => $uploaded_files['files']['tmp_name'][$source][$i],
+ 'filesize' => $uploaded_files['files']['size'][$source][$i],
+ );
+ $values['filemime'] = file_get_mimetype($values['filename']);
+ $file = entity_create('file', $values);
+
+ $extensions = '';
+ if (isset($validators['file_validate_extensions'])) {
+ if (isset($validators['file_validate_extensions'][0])) {
+ // Build the list of non-munged extensions if the caller provided them.
+ $extensions = $validators['file_validate_extensions'][0];
+ }
+ else {
+ // If 'file_validate_extensions' is set and the list is empty then the
+ // caller wants to allow any extension. In this case we have to remove the
+ // validator or else it will reject all extensions.
+ unset($validators['file_validate_extensions']);
+ }
}
else {
- // If 'file_validate_extensions' is set and the list is empty then the
- // caller wants to allow any extension. In this case we have to remove the
- // validator or else it will reject all extensions.
- unset($validators['file_validate_extensions']);
+ // No validator was provided, so add one using the default list.
+ // Build a default non-munged safe list for file_munge_filename().
+ $extensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
+ $validators['file_validate_extensions'] = array();
+ $validators['file_validate_extensions'][0] = $extensions;
}
- }
- else {
- // No validator was provided, so add one using the default list.
- // Build a default non-munged safe list for file_munge_filename().
- $extensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
- $validators['file_validate_extensions'] = array();
- $validators['file_validate_extensions'][0] = $extensions;
- }
-
- if (!empty($extensions)) {
- // Munge the filename to protect against possible malicious extension hiding
- // within an unknown file type (ie: filename.html.foo).
- $file->filename = file_munge_filename($file->filename, $extensions);
- }
-
- // Rename potentially executable files, to help prevent exploits (i.e. will
- // rename filename.php.foo and filename.php to filename.php.foo.txt and
- // filename.php.txt, respectively). Don't rename if 'allow_insecure_uploads'
- // evaluates to TRUE.
- if (!config('system.file')->get('allow_insecure_uploads') && preg_match('/\.(php|pl|py|cgi|asp|js)(\.|$)/i', $file->filename) && (substr($file->filename, -4) != '.txt')) {
- $file->filemime = 'text/plain';
- $file->uri .= '.txt';
- $file->filename .= '.txt';
- // The .txt extension may not be in the allowed list of extensions. We have
- // to add it here or else the file upload will fail.
+
if (!empty($extensions)) {
- $validators['file_validate_extensions'][0] .= ' txt';
- drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', array('%filename' => $file->filename)));
+ // Munge the filename to protect against possible malicious extension
+ // hiding within an unknown file type (ie: filename.html.foo).
+ $file->filename = file_munge_filename($file->filename, $extensions);
}
- }
- // If the destination is not provided, use the temporary directory.
- if (empty($destination)) {
- $destination = 'temporary://';
- }
+ // Rename potentially executable files, to help prevent exploits (i.e. will
+ // rename filename.php.foo and filename.php to filename.php.foo.txt and
+ // filename.php.txt, respectively). Don't rename if 'allow_insecure_uploads'
+ // evaluates to TRUE.
+ if (!config('system.file')->get('allow_insecure_uploads') && preg_match('/\.(php|pl|py|cgi|asp|js)(\.|$)/i', $file->filename) && (substr($file->filename, -4) != '.txt')) {
+ $file->filemime = 'text/plain';
+ $file->uri .= '.txt';
+ $file->filename .= '.txt';
+ // The .txt extension may not be in the allowed list of extensions. We have
+ // to add it here or else the file upload will fail.
+ if (!empty($extensions)) {
+ $validators['file_validate_extensions'][0] .= ' txt';
+ drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', array('%filename' => $file->filename)));
+ }
+ }
- // Assert that the destination contains a valid stream.
- $destination_scheme = file_uri_scheme($destination);
- if (!file_stream_wrapper_valid_scheme($destination_scheme)) {
- drupal_set_message(t('The file could not be uploaded because the destination %destination is invalid.', array('%destination' => $destination)), 'error');
- return FALSE;
- }
+ // If the destination is not provided, use the temporary directory.
+ if (empty($destination)) {
+ $destination = 'temporary://';
+ }
- $file->source = $source;
- // A URI may already have a trailing slash or look like "public://".
- if (substr($destination, -1) != '/') {
- $destination .= '/';
- }
- $file->destination = file_destination($destination . $file->filename, $replace);
- // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and
- // there's an existing file so we need to bail.
- if ($file->destination === FALSE) {
- drupal_set_message(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $source, '%directory' => $destination)), 'error');
- return FALSE;
- }
+ // Assert that the destination contains a valid stream.
+ $destination_scheme = file_uri_scheme($destination);
+ if (!file_stream_wrapper_valid_scheme($destination_scheme)) {
+ drupal_set_message(t('The file could not be uploaded because the destination %destination is invalid.', array('%destination' => $destination)), 'error');
+ $files[$i] = FALSE;
+ continue;
+ }
- // Add in our check of the the file name length.
- $validators['file_validate_name_length'] = array();
+ $file->source = $source;
+ // A file URI may already have a trailing slash or look like "public://".
+ if (substr($destination, -1) != '/') {
+ $destination .= '/';
+ }
+ $file->destination = file_destination($destination . $file->filename, $replace);
+ // If file_destination() returns FALSE then $replace === FILE_EXISTS_ERROR and
+ // there's an existing file so we need to bail.
+ if ($file->destination === FALSE) {
+ drupal_set_message(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $source, '%directory' => $destination)), 'error');
+ $files[$i] = FALSE;
+ continue;
+ }
- // Call the validation functions specified by this function's caller.
- $errors = file_validate($file, $validators);
+ // Add in our check of the the file name length.
+ $validators['file_validate_name_length'] = array();
- // Check for errors.
- if (!empty($errors)) {
- $message = t('The specified file %name could not be uploaded.', array('%name' => $file->filename));
- if (count($errors) > 1) {
- $message .= theme('item_list', array('items' => $errors));
+ // Call the validation functions specified by this function's caller.
+ $errors = file_validate($file, $validators);
+
+ // Check for errors.
+ if (!empty($errors)) {
+ $message = t('The specified file %name could not be uploaded.', array('%name' => $file->filename));
+ if (count($errors) > 1) {
+ $message .= theme('item_list', array('items' => $errors));
+ }
+ else {
+ $message .= ' ' . array_pop($errors);
+ }
+ form_set_error($source, $message);
+ $files[$i] = FALSE;
+ continue;
}
- else {
- $message .= ' ' . array_pop($errors);
+
+ // Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary
+ // directory. This overcomes open_basedir restrictions for future file
+ // operations.
+ $file->uri = $file->destination;
+ if (!drupal_move_uploaded_file($uploaded_files['files']['tmp_name'][$source][$i], $file->uri)) {
+ form_set_error($source, t('File upload error. Could not move uploaded file.'));
+ watchdog('file', 'Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->uri));
+ $files[$i] = FALSE;
+ continue;
}
- form_set_error($source, $message);
- return FALSE;
- }
- // Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary
- // directory. This overcomes open_basedir restrictions for future file
- // operations.
- $file->uri = $file->destination;
- if (!drupal_move_uploaded_file($_FILES['files']['tmp_name'][$source], $file->uri)) {
- form_set_error($source, t('File upload error. Could not move uploaded file.'));
- watchdog('file', 'Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->uri));
- return FALSE;
- }
+ // Set the permissions on the new file.
+ drupal_chmod($file->uri);
- // Set the permissions on the new file.
- drupal_chmod($file->uri);
-
- // If we are replacing an existing file re-use its database record.
- if ($replace == FILE_EXISTS_REPLACE) {
- $existing_files = entity_load_multiple_by_properties('file', array('uri' => $file->uri));
- if (count($existing_files)) {
- $existing = reset($existing_files);
- $file->fid = $existing->fid;
+ // If we are replacing an existing file re-use its database record.
+ if ($replace == FILE_EXISTS_REPLACE) {
+ $existing_files = entity_load_multiple_by_properties('file', array('uri' => $file->uri));
+ if (count($existing_files)) {
+ $existing = reset($existing_files);
+ $file->fid = $existing->fid;
+ }
}
+
+ // If we made it this far it's safe to record this file in the database.
+ $file->save();
+ $files[$i] = $file;
}
- // If we made it this far it's safe to record this file in the database.
- $file->save();
- // Add file to the cache.
- $upload_cache[$source] = $file;
- return $file;
+ // Add files to the cache.
+ $upload_cache[$source] = $files;
+
+ return isset($delta) ? $files[$delta] : $files;
}
/**
@@ -1390,7 +1420,7 @@ function file_scan_directory($dir, $mask, $options = array(), $depth = 0) {
// Avoid warnings when opendir does not have the permissions to open a
// directory.
if (is_dir($dir)) {
- if($handle = @opendir($dir)) {
+ if ($handle = @opendir($dir)) {
while (FALSE !== ($filename = readdir($handle))) {
// Skip this file if it matches the nomask or starts with a dot.
if ($filename[0] != '.' && !preg_match($options['nomask'], $filename)) {
diff --git a/core/includes/form.inc b/core/includes/form.inc
index ca9e614..b9180b2 100644
--- a/core/includes/form.inc
+++ b/core/includes/form.inc
@@ -4609,6 +4609,17 @@ function form_pre_render_file($element) {
}
/**
+ * Processes a file upload element, make use of #multiple if present.
+ */
+function form_process_file($element) {
+ if ($element['#multiple']) {
+ $element['#attributes'] = array('multiple' => 'multiple');
+ $element['#name'] .= '[]';
+ }
+ return $element;
+}
+
+/**
* Returns HTML for a form element.
*
* Each form element is wrapped in a DIV container having the following CSS
diff --git a/core/modules/aggregator/aggregator.admin.inc b/core/modules/aggregator/aggregator.admin.inc
index 3eee1f0..27f10e7 100644
--- a/core/modules/aggregator/aggregator.admin.inc
+++ b/core/modules/aggregator/aggregator.admin.inc
@@ -188,7 +188,7 @@ function aggregator_form_opml_validate($form, &$form_state) {
function aggregator_form_opml_submit($form, &$form_state) {
$data = '';
$validators = array('file_validate_extensions' => array('opml xml'));
- if ($file = file_save_upload('upload', $validators)) {
+ if ($file = file_save_upload('upload', $validators, FALSE, 0)) {
$data = file_get_contents($file->uri);
}
else {
diff --git a/core/modules/file/file.field.inc b/core/modules/file/file.field.inc
index f3f2c0a..de03e50 100644
--- a/core/modules/file/file.field.inc
+++ b/core/modules/file/file.field.inc
@@ -389,7 +389,7 @@ function file_field_widget_value($element, $input = FALSE, $form_state) {
// Ensure that all the required properties are returned even if empty.
$return += array(
- 'fid' => 0,
+ 'fids' => array(),
'display' => 1,
'description' => '',
);
@@ -398,6 +398,47 @@ function file_field_widget_value($element, $input = FALSE, $form_state) {
}
/**
+ * Validation callback for upload element on file widget. Checks if user has
+ * uploaded more files than allowed.
+ *
+ * This validator is used only when cardinality not set to 1 or unlimited.
+ */
+function file_field_widget_multiple_count_validate($element, &$form_state, $form) {
+ $parents = $element['#parents'];
+ $values = NestedArray::getValue($form_state['values'], $parents);
+
+ array_pop($parents);
+ $current = count(element_children(NestedArray::getValue($form, $parents))) - 1;
+
+ $field = field_info_field($element['#field_name']);
+ $uploaded = count($values['fids']);
+ $count = $uploaded + $current;
+ if ($count > $field['cardinality']) {
+ $keep = $uploaded - $count + $field['cardinality'];
+ $removed_files = array_slice($values['fids'], $keep);
+ $removed_names = array();
+ foreach ($removed_files as $fid) {
+ $file = file_load($fid);
+ $removed_names[] = $file->filename;
+ }
+ drupal_set_message(
+ t(
+ 'Field %field can only hold @max values but there were @count uploaded. The following files have been omitted as a result: %list.',
+ array(
+ '%field' => $field['field_name'],
+ '@max' => $field['cardinality'],
+ '@count' => $keep,
+ '%list' => implode(', ', $removed_names),
+ )
+ ),
+ 'warning'
+ );
+ $values['fids'] = array_slice($values['fids'], 0, $keep);
+ NestedArray::setValue($form_state['values'], $element['#parents'], $values);
+ }
+}
+
+/**
* Render API callback: Processes a file_generic field element.
*
* Expands the file_generic type to include the description and display fields.
@@ -406,7 +447,7 @@ function file_field_widget_value($element, $input = FALSE, $form_state) {
*/
function file_field_widget_process($element, &$form_state, $form) {
$item = $element['#value'];
- $item['fid'] = $element['fid']['#value'];
+ $item['fids'] = $element['fids']['#value'];
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
@@ -415,9 +456,9 @@ function file_field_widget_process($element, &$form_state, $form) {
$element['#theme'] = 'file_widget';
// Add the display field if enabled.
- if (!empty($field['settings']['display_field']) && $item['fid']) {
+ if (!empty($field['settings']['display_field']) && $item['fids']) {
$element['display'] = array(
- '#type' => empty($item['fid']) ? 'hidden' : 'checkbox',
+ '#type' => empty($item['fids']) ? 'hidden' : 'checkbox',
'#title' => t('Include file in display'),
'#value' => isset($item['display']) ? $item['display'] : $field['settings']['display_default'],
'#attributes' => array('class' => array('file-display')),
@@ -431,7 +472,7 @@ function file_field_widget_process($element, &$form_state, $form) {
}
// Add the description field if enabled.
- if (!empty($instance['settings']['description_field']) && $item['fid']) {
+ if (!empty($instance['settings']['description_field']) && $item['fids']) {
$config = config('file.settings');
$element['description'] = array(
'#type' => $config->get('description.type'),
@@ -561,18 +602,34 @@ function file_field_widget_submit($form, &$form_state) {
$langcode = $element['#language'];
$parents = $element['#field_parents'];
- $submitted_values = NestedArray::getValue($form_state['values'], array_slice($button['#array_parents'], 0, -2));
+ $submitted_values = NestedArray::getValue($form_state['values'], array_slice($button['#parents'], 0, -2));
foreach ($submitted_values as $delta => $submitted_value) {
- if (!$submitted_value['fid']) {
+ if (empty($submitted_value['fids'])) {
unset($submitted_values[$delta]);
}
}
+ // If there are more files uploaded via the same widget, we have to separate
+ // them, as we display each file in it's own widget.
+ $new_values = array();
+ foreach ($submitted_values as $delta => $submitted_value) {
+ if (is_array($submitted_value['fids'])) {
+ foreach ($submitted_value['fids'] as $fid) {
+ $new_value = $submitted_value;
+ $new_value['fids'] = array($fid);
+ $new_values[] = $new_value;
+ }
+ }
+ else {
+ $new_value = $submitted_value;
+ }
+ }
+
// Re-index deltas after removing empty items.
- $submitted_values = array_values($submitted_values);
+ $submitted_values = array_values($new_values);
// Update form_state values.
- NestedArray::setValue($form_state['values'], array_slice($button['#array_parents'], 0, -2), $submitted_values);
+ NestedArray::setValue($form_state['values'], array_slice($button['#parents'], 0, -2), $submitted_values);
// Update items.
$field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
@@ -595,9 +652,10 @@ function theme_file_widget($variables) {
// The "form-managed-file" class is required for proper Ajax functionality.
$output .= '<div class="file-widget form-managed-file clearfix">';
- if ($element['fid']['#value'] != 0) {
+ if (!empty($element['fids']['#value'])) {
// Add the file size after the file name.
- $element['filename']['#markup'] .= ' <span class="file-size">(' . format_size($element['#file']->filesize) . ')</span> ';
+ $file = reset($element['#files']);
+ $element['file_' . $file->fid]['filename']['#markup'] .= ' <span class="file-size">(' . format_size($file->filesize) . ')</span> ';
}
$output .= drupal_render_children($element);
$output .= '</div>';
@@ -644,7 +702,7 @@ function theme_file_widget_multiple($variables) {
$rows = array();
foreach ($widgets as $key => &$widget) {
// Save the uploading row for last.
- if ($widget['#file'] == FALSE) {
+ if (empty($widget['#files'])) {
$widget['#title'] = $element['#file_upload_title'];
$widget['#description'] = $element['#file_upload_description'];
continue;
@@ -726,6 +784,7 @@ function theme_file_widget_multiple($variables) {
function theme_file_upload_help($variables) {
$description = $variables['description'];
$upload_validators = $variables['upload_validators'];
+ $cardinality = $variables['cardinality'];
$descriptions = array();
@@ -738,6 +797,14 @@ function theme_file_upload_help($variables) {
if (isset($upload_validators['file_validate_extensions'])) {
$descriptions[] = t('Allowed file types: !extensions.', array('!extensions' => '<strong>' . check_plain($upload_validators['file_validate_extensions'][0]) . '</strong>'));
}
+ if (isset($cardinality)) {
+ if ($cardinality == -1) {
+ $descriptions[] = t('Unlimited number of files can be uploaded to this field.');
+ }
+ else {
+ $descriptions[] = format_plural($cardinality, 'This field can store only one file.', 'This field can store at most @count files.');
+ }
+ }
if (isset($upload_validators['file_validate_image_resolution'])) {
$max = $upload_validators['file_validate_image_resolution'][0];
$min = $upload_validators['file_validate_image_resolution'][1];
diff --git a/core/modules/file/file.install b/core/modules/file/file.install
index 36ce089..3ca855e 100644
--- a/core/modules/file/file.install
+++ b/core/modules/file/file.install
@@ -246,7 +246,7 @@ function file_update_8000() {
));
}
-/**
+/*
* Convert the 'id' column in {file_usage} to accept UUIDs.
*/
function file_update_8001() {
@@ -259,3 +259,32 @@ function file_update_8001() {
);
db_change_field('file_usage', 'id', 'id', $spec);
}
+
+/**
+* Convert image field's default image configuration to the new format.
+*/
+function file_update_8002() {
+ if (module_exists('field_sql_storage')) {
+ $fields = field_read_fields(array('type' => 'image'), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
+ foreach ($fields as $field) {
+ if (!empty($field['settings']['default_image'])) {
+ $field['settings']['default_image'] = array($field['settings']['default_image']);
+ }
+ else {
+ $field['settings']['default_image'] = array();
+ }
+ field_update_field($field);
+
+ $instances = field_read_instances(array('field_name' => $field['field_name']));
+ foreach ($instances as $instance) {
+ if (!empty($instance['settings']['default_image'])) {
+ $instance['settings']['default_image'] = array($instance['settings']['default_image']);
+ }
+ else {
+ $instance['settings']['default_image'] = array();
+ }
+ field_update_instance($instance);
+ }
+ }
+ }
+}
diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index 69fcbf3..3fcd034 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -81,6 +81,7 @@ function file_element_info() {
'#upload_validators' => array(),
'#upload_location' => NULL,
'#size' => 22,
+ '#multiple' => FALSE,
'#extended' => FALSE,
'#attached' => array(
'library' => array(array('file','drupal.file')),
@@ -610,7 +611,7 @@ function file_theme() {
'variables' => array('items' => NULL),
),
'file_upload_help' => array(
- 'variables' => array('description' => NULL, 'upload_validators' => NULL),
+ 'variables' => array('description' => NULL, 'upload_validators' => NULL, 'cardinality' => NULL),
),
);
}
@@ -870,11 +871,15 @@ function file_managed_file_process($element, &$form_state, $form) {
// Append the '-upload' to the #id so the field label's 'for' attribute
// corresponds with the file element.
$element['#id'] .= '-upload';
- $fid = isset($element['#value']['fid']) ? $element['#value']['fid'] : 0;
+
+ // This is used sometimes so let's implode it just once.
+ $parents_prefix = implode('_', $element['#parents']);
+
+ $fids = isset($element['#value']['fids']) ? $element['#value']['fids'] : array();
// Set some default element properties.
$element['#progress_indicator'] = empty($element['#progress_indicator']) ? 'none' : $element['#progress_indicator'];
- $element['#file'] = $fid ? file_load($fid) : FALSE;
+ $element['#files'] = !empty($fids) ? file_load_multiple($fids) : FALSE;
$element['#tree'] = TRUE;
$ajax_settings = array(
@@ -889,7 +894,7 @@ function file_managed_file_process($element, &$form_state, $form) {
// Set up the buttons first since we need to check if they were clicked.
$element['upload_button'] = array(
- '#name' => implode('_', $element['#parents']) . '_upload_button',
+ '#name' => $parents_prefix . '_upload_button',
'#type' => 'submit',
'#value' => t('Upload'),
'#validate' => array(),
@@ -905,19 +910,19 @@ function file_managed_file_process($element, &$form_state, $form) {
$ajax_settings['progress']['message'] = NULL;
$ajax_settings['effect'] = 'none';
$element['remove_button'] = array(
- '#name' => implode('_', $element['#parents']) . '_remove_button',
+ '#name' => $parents_prefix . '_remove_button',
'#type' => 'submit',
- '#value' => t('Remove'),
+ '#value' => $element['#multiple'] ? t('Remove selected') : t('Remove'),
'#validate' => array(),
'#submit' => array('file_managed_file_submit'),
'#limit_validation_errors' => array($element['#parents']),
'#ajax' => $ajax_settings,
- '#weight' => -5,
+ '#weight' => 1,
);
- $element['fid'] = array(
+ $element['fids'] = array(
'#type' => 'hidden',
- '#value' => $fid,
+ '#value' => $fids,
);
// Add progress bar support to the upload if possible.
@@ -951,21 +956,32 @@ function file_managed_file_process($element, &$form_state, $form) {
// The file upload field itself.
$element['upload'] = array(
- '#name' => 'files[' . implode('_', $element['#parents']) . ']',
+ '#name' => 'files[' . $parents_prefix . ']',
'#type' => 'file',
'#title' => t('Choose a file'),
'#title_display' => 'invisible',
'#size' => $element['#size'],
+ '#multiple' => $element['#multiple'],
'#theme_wrappers' => array(),
'#weight' => -10,
);
- if ($fid && $element['#file']) {
- $element['filename'] = array(
- '#type' => 'markup',
- '#markup' => theme('file_link', array('file' => $element['#file'])) . ' ',
- '#weight' => -10,
- );
+ if (!empty($fids) && $element['#files']) {
+ foreach ($element['#files'] as $delta => $file) {
+ if ($element['#multiple']) {
+ $element['file_' . $delta]['selected'] = array(
+ '#type' => 'checkbox',
+ '#title' => theme('file_link', array('file' => $file)) . ' ',
+ );
+ }
+ else {
+ $element['file_' . $delta]['filename'] = array(
+ '#type' => 'markup',
+ '#markup' => theme('file_link', array('file' => $file)) . ' ',
+ '#weight' => -10,
+ );
+ }
+ }
}
// Add the extension list to the page as JavaScript settings.
@@ -992,28 +1008,25 @@ function file_managed_file_process($element, &$form_state, $form) {
* This function is assigned as a #value_callback in file_element_info().
*/
function file_managed_file_value(&$element, $input = FALSE, $form_state = NULL) {
- $fid = 0;
-
- // Find the current value of this field from the form state.
- $form_state_fid = $form_state['values'];
- foreach ($element['#parents'] as $parent) {
- $form_state_fid = isset($form_state_fid[$parent]) ? $form_state_fid[$parent] : 0;
- }
-
- if ($element['#extended'] && isset($form_state_fid['fid'])) {
- $fid = $form_state_fid['fid'];
- }
- elseif (is_numeric($form_state_fid)) {
- $fid = $form_state_fid;
+ // Find the current value of this field.
+ $fids = !empty($input['fids']) ? explode(' ', $input['fids']) : array();
+ foreach ($fids as $key => $fid) {
+ $fids[$key] = (int) $fid;
}
// Process any input and save new uploads.
if ($input !== FALSE) {
+ $input['fids'] = $fids;
$return = $input;
// Uploads take priority over all other values.
- if ($file = file_managed_file_save_upload($element)) {
- $fid = $file->fid;
+ if ($files = file_managed_file_save_upload($element)) {
+ if ($element['#multiple']) {
+ $fids = array_merge($fids, array_keys($files));
+ }
+ else {
+ $fids = array_keys($files);
+ }
}
else {
// Check for #filefield_value_callback values.
@@ -1025,9 +1038,15 @@ function file_managed_file_value(&$element, $input = FALSE, $form_state = NULL)
$callback($element, $input, $form_state);
}
}
- // Load file if the FID has changed to confirm it exists.
- if (isset($input['fid']) && $file = file_load($input['fid'])) {
- $fid = $file->fid;
+
+ // Load files if the FIDs have changed to confirm they exist.
+ if (!empty($input['fids'])) {
+ $fids = array();
+ foreach ($input['fids'] as $key => $fid) {
+ if ($file = file_load($fid)) {
+ $fids[] = $file->fid;
+ }
+ }
}
}
}
@@ -1035,22 +1054,26 @@ function file_managed_file_value(&$element, $input = FALSE, $form_state = NULL)
// If there is no input, set the default value.
else {
if ($element['#extended']) {
- $default_fid = isset($element['#default_value']['fid']) ? $element['#default_value']['fid'] : 0;
- $return = isset($element['#default_value']) ? $element['#default_value'] : array('fid' => 0);
+ $default_fids = isset($element['#default_value']['fids']) ? $element['#default_value']['fids'] : array();
+ $return = isset($element['#default_value']) ? $element['#default_value'] : array('fids' => array());
}
else {
- $default_fid = isset($element['#default_value']) ? $element['#default_value'] : 0;
- $return = array('fid' => 0);
+ $default_fids = isset($element['#default_value']) ? $element['#default_value'] : array();
+ $return = array('fids' => array());
}
// Confirm that the file exists when used as a default value.
- if ($default_fid && $file = file_load($default_fid)) {
- $fid = $file->fid;
+ if (!empty($default_fids)) {
+ $fids = array();
+ foreach ($default_fids as $key => $fid) {
+ if ($file = file_load($fid)) {
+ $fids[] = $file->fid;
+ }
+ }
}
}
- $return['fid'] = $fid;
-
+ $return['fids'] = $fids;
return $return;
}
@@ -1065,28 +1088,31 @@ function file_managed_file_validate(&$element, &$form_state) {
// references. This prevents unmanaged files from being deleted if this
// item were to be deleted.
$clicked_button = end($form_state['triggering_element']['#parents']);
- if ($clicked_button != 'remove_button' && !empty($element['fid']['#value'])) {
- if ($file = file_load($element['fid']['#value'])) {
- if ($file->status == FILE_STATUS_PERMANENT) {
- $references = file_usage()->listUsage($file);
- if (empty($references)) {
- form_error($element, t('The file used in the !name field may not be referenced.', array('!name' => $element['#title'])));
+ if ($clicked_button != 'remove_button' && !empty($element['fids']['#value'])) {
+ $fids = $element['fids']['#value'];
+ foreach ($fids as $fid) {
+ if ($file = file_load($fid)) {
+ if ($file->status == FILE_STATUS_PERMANENT) {
+ $references = file_usage()->listUsage($file);
+ if (empty($references)) {
+ form_error($element, t('The file used in the !name field may not be referenced.', array('!name' => $element['#title'])));
+ }
}
}
- }
- else {
- form_error($element, t('The file referenced by the !name field does not exist.', array('!name' => $element['#title'])));
+ else {
+ form_error($element, t('The file referenced by the !name field does not exist.', array('!name' => $element['#title'])));
+ }
}
}
// Check required property based on the FID.
- if ($element['#required'] && empty($element['fid']['#value']) && !in_array($clicked_button, array('upload_button', 'remove_button'))) {
+ if ($element['#required'] && empty($element['fids']['#value']) && !in_array($clicked_button, array('upload_button', 'remove_button'))) {
form_error($element['upload'], t('!name field is required.', array('!name' => $element['#title'])));
}
- // Consolidate the array value of this field to a single FID.
+ // Consolidate the array value of this field to array of FIDs.
if (!$element['#extended']) {
- form_set_value($element, $element['fid']['#value'], $form_state);
+ form_set_value($element, $element['fids']['#value'], $form_state);
}
}
@@ -1107,11 +1133,28 @@ function file_managed_file_submit($form, &$form_state) {
// button was clicked. Action is needed here for the remove button, because we
// only remove a file in response to its remove button being clicked.
if ($button_key == 'remove_button') {
- // If it's a temporary file we can safely remove it immediately, otherwise
- // it's up to the implementing module to remove usages of files to have them
- // removed.
- if ($element['#file'] && $element['#file']->status == 0) {
- file_delete($element['#file']->fid);
+ $fids = array_keys($element['#files']);
+ // Get files that will be removed.
+ if ($element['#multiple']) {
+ $remove_fids = array();
+ foreach (element_children($element) as $name) {
+ if (strpos($name, 'file_') === 0 && $element[$name]['selected']['#value']) {
+ $remove_fids[] = (int) substr($name, 5);
+ }
+ }
+ $fids = array_diff($fids, $remove_fids);
+ }
+ else {
+ $remove_fids = $fids;
+ }
+
+ foreach ($remove_fids as $fid) {
+ // If it's a temporary file we can safely remove it immediately, otherwise
+ // it's up to the implementing module to remove usages of files to have them
+ // removed.
+ if ($element['#files'][$fid] && $element['#files'][$fid]->status == 0) {
+ file_delete($element['#files'][$fid]->fid);
+ }
}
// Update both $form_state['values'] and $form_state['input'] to reflect
// that the file has been removed, so that the form is rebuilt correctly.
@@ -1120,9 +1163,8 @@ function file_managed_file_submit($form, &$form_state) {
// when the managed_file element is part of a field widget.
// $form_state['input'] must be updated so that file_managed_file_value()
// has correct information during the rebuild.
- $values_element = $element['#extended'] ? $element['fid'] : $element;
- form_set_value($values_element, NULL, $form_state);
- NestedArray::setValue($form_state['input'], $values_element['#parents'], NULL);
+ form_set_value($element['fids'], implode(' ', $fids), $form_state);
+ NestedArray::setValue($form_state['input'], $element['fids']['#parents'], implode(' ', $fids));
}
// Set the form to rebuild so that $form is correctly updated in response to
@@ -1141,8 +1183,8 @@ function file_managed_file_submit($form, &$form_state) {
* The FAPI element whose values are being saved.
*
* @return
- * The file entity representing the file that was saved, or FALSE if no file
- * was saved.
+ * An array of file entities for each file that was saved, keyed by its file
+ * ID, or FALSE if no files were saved.
*/
function file_managed_file_save_upload($element) {
$upload_name = implode('_', $element['#parents']);
@@ -1157,13 +1199,24 @@ function file_managed_file_save_upload($element) {
return FALSE;
}
- if (!$file = file_save_upload($upload_name, $element['#upload_validators'], $destination)) {
- watchdog('file', 'The file upload failed. %upload', array('%upload' => $upload_name));
- form_set_error($upload_name, t('The file in the !name field was unable to be uploaded.', array('!name' => $element['#title'])));
- return FALSE;
+ // Save attached files to the database.
+ $files_uploaded = $element['#multiple'] && count(array_filter($_FILES['files']['name'][$upload_name])) > 0;
+ $files_uploaded |= !$element['#multiple'] && !empty($_FILES['files']['name'][$upload_name]);
+ if ($files_uploaded) {
+ if (!$files = file_save_upload($upload_name, $element['#upload_validators'], $destination)) {
+ watchdog('file', 'The file upload failed. %upload', array('%upload' => $upload_name));
+ form_set_error($upload_name, t('Files in the !name field were unable to be uploaded.', array('!name' => $element['#title'])));
+ return array();
+ }
+
+ // Value callback expects FIDs to be keys.
+ $files = array_filter($files);
+ $fids = array_map(function($file) { return $file->fid; }, $files);
+
+ return empty($files) ? array() : array_combine($fids, $files);
}
- return $file;
+ return array();
}
/**
@@ -1218,9 +1271,11 @@ function theme_file_managed_file($variables) {
*/
function file_managed_file_pre_render($element) {
// If we already have a file, we don't want to show the upload controls.
- if (!empty($element['#value']['fid'])) {
- $element['upload']['#access'] = FALSE;
- $element['upload_button']['#access'] = FALSE;
+ if (!empty($element['#value']['fids'])) {
+ if (!$element['#multiple']) {
+ $element['upload']['#access'] = FALSE;
+ $element['upload_button']['#access'] = FALSE;
+ }
}
// If we don't already have a file, there is nothing to remove.
else {
diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php b/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php
index 94031b2..4eebf6c 100644
--- a/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php
+++ b/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php
@@ -156,7 +156,7 @@ class FileWidget extends WidgetBase {
// field. These are added here so that they may be referenced easily
// through a hook_form_alter().
$elements['#file_upload_title'] = t('Add a new file');
- $elements['#file_upload_description'] = theme('file_upload_help', array('description' => '', 'upload_validators' => $elements[0]['#upload_validators']));
+ $elements['#file_upload_description'] = theme('file_upload_help', array('description' => '', 'upload_validators' => $elements[0]['#upload_validators'], 'cardinality' => $this->field['cardinality']));
}
return $elements;
@@ -167,7 +167,7 @@ class FileWidget extends WidgetBase {
*/
public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
$defaults = array(
- 'fid' => 0,
+ 'fids' => array(),
'display' => !empty($this->field['settings']['display_default']),
'description' => '',
);
@@ -187,13 +187,45 @@ class FileWidget extends WidgetBase {
);
$element['#weight'] = $delta;
+
+ // Field stores FID value in a single mode, so we need to transform it for
+ // form element to recognize it correctly.
+ if (!isset($items[$delta]['fids']) && isset($items[$delta]['fid'])) {
+ $items[$delta]['fids'][0] = $items[$delta]['fid'];
+ }
$element['#default_value'] = !empty($items[$delta]) ? $items[$delta] : $defaults;
- if (empty($element['#default_value']['fid'])) {
- $element['#description'] = theme('file_upload_help', array('description' => $element['#description'], 'upload_validators' => $element['#upload_validators']));
+ $default_fids = $element['#extended'] ? $element['#default_value']['fids'] : $element['#default_value'];
+ if (empty($default_fids)) {
+ $cardinality = isset($this->field['cardinality']) ? $this->field['cardinality'] : 1;
+ $element['#description'] = theme('file_upload_help', array('description' => $element['#description'], 'upload_validators' => $element['#upload_validators'], 'cardinality' => $cardinality));
+ $element['#multiple'] = $cardinality != 1 ? TRUE : FALSE;
+ if ($cardinality != 1 && $cardinality != -1) {
+ $element['#element_validate'] = array('file_field_widget_multiple_count_validate');
+ }
}
return $element;
}
+ /**
+ * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues().
+ */
+ public function massageFormValues(array $values, array $form, array &$form_state) {
+ // Since file upload widget now supports uploads of more than one file at a
+ // time it always returns an array of fids. We have to translate this to a
+ // single fid, as field expects single value.
+ $new_values = array();
+ foreach ($values as &$value) {
+ foreach ($value['fids'] as $fid) {
+ $new_value = $value;
+ $new_value['fid'] = $fid;
+ unset($new_value['fids']);
+ $new_values[] = $new_value;
+ }
+ }
+
+ return $new_values;
+ }
+
}
diff --git a/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php b/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php
index 94eaf39..8cff731 100644
--- a/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php
+++ b/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php
@@ -149,7 +149,12 @@ abstract class FileFieldTestBase extends WebTestBase {
}
// Attach a file to the node.
- $edit['files[' . $field_name . '_' . $langcode . '_0]'] = drupal_realpath($file->uri);
+ $field = field_info_field($field_name);
+ $name = 'files[' . $field_name . '_' . $langcode . '_0]';
+ if ($field['cardinality'] != 1) {
+ $name .= '[]';
+ }
+ $edit[$name] = drupal_realpath($file->uri);
$this->drupalPost("node/$nid/edit", $edit, t('Save and keep published'));
return $nid;
diff --git a/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php b/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php
index 0ea1337..041e166 100644
--- a/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php
+++ b/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php
@@ -111,7 +111,7 @@ class FileFieldWidgetTest extends FileFieldTestBase {
$this->drupalGet("node/add/$type_name");
foreach (array($field_name2, $field_name) as $each_field_name) {
for ($delta = 0; $delta < 3; $delta++) {
- $edit = array('files[' . $each_field_name . '_' . LANGUAGE_NOT_SPECIFIED . '_' . $delta . ']' => drupal_realpath($test_file->uri));
+ $edit = array('files[' . $each_field_name . '_' . LANGUAGE_NOT_SPECIFIED . '_' . $delta . '][]' => drupal_realpath($test_file->uri));
// If the Upload button doesn't exist, drupalPost() will automatically
// fail with an assertion message.
$this->drupalPost(NULL, $edit, t('Upload'));
diff --git a/core/modules/file/lib/Drupal/file/Tests/FileManagedFileElementTest.php b/core/modules/file/lib/Drupal/file/Tests/FileManagedFileElementTest.php
index 9865314..6e43fc1 100644
--- a/core/modules/file/lib/Drupal/file/Tests/FileManagedFileElementTest.php
+++ b/core/modules/file/lib/Drupal/file/Tests/FileManagedFileElementTest.php
@@ -30,73 +30,123 @@ class FileManagedFileElementTest extends FileFieldTestBase {
$this->drupalGet('file/test');
$this->assertFieldByXpath('//input[@name="files[nested_file]" and @size="13"]', NULL, 'The custom #size attribute is passed to the child upload element.');
- // Perform the tests with all permutations of $form['#tree'] and
- // $element['#extended'].
+ // Perform the tests with all permutations of $form['#tree'],
+ // $element['#extended'], and $element['#multiple'].
+ $test_file = $this->getTestFile('text');
foreach (array(0, 1) as $tree) {
foreach (array(0, 1) as $extended) {
- $test_file = $this->getTestFile('text');
- $path = 'file/test/' . $tree . '/' . $extended;
- $input_base_name = $tree ? 'nested_file' : 'file';
-
- // Submit without a file.
- $this->drupalPost($path, array(), t('Save'));
- $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), t('Submitted without a file.'));
-
- // Submit a new file, without using the Upload button.
- $last_fid_prior = $this->getLastFileId();
- $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
- $this->drupalPost($path, $edit, t('Save'));
- $last_fid = $this->getLastFileId();
- $this->assertTrue($last_fid > $last_fid_prior, t('New file got saved.'));
- $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), t('Submit handler has correct file info.'));
-
- // Submit no new input, but with a default file.
- $this->drupalPost($path . '/' . $last_fid, array(), t('Save'));
- $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), t('Empty submission did not change an existing file.'));
-
- // Now, test the Upload and Remove buttons, with and without Ajax.
- foreach (array(FALSE, TRUE) as $ajax) {
- // Upload, then Submit.
+ foreach (array(0, 1) as $multiple) {
+ $path = 'file/test/' . $tree . '/' . $extended . '/' . $multiple;
+ $input_base_name = $tree ? 'nested_file' : 'file';
+ $file_field_name = $multiple ? 'files[' . $input_base_name . '][]' : 'files[' . $input_base_name . ']';
+
+ // Submit without a file.
+ $this->drupalPost($path, array(), t('Save'));
+ $this->assertRaw(t('The file ids are %fids.', array('%fids' => implode(',', array()))), t('Submitted without a file.'));
+
+ // Submit a new file, without using the Upload button.
$last_fid_prior = $this->getLastFileId();
- $this->drupalGet($path);
- $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
- if ($ajax) {
- $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
- }
- else {
- $this->drupalPost(NULL, $edit, t('Upload'));
- }
+ $edit = array($file_field_name => drupal_realpath($test_file->uri));
+ $this->drupalPost($path, $edit, t('Save'));
$last_fid = $this->getLastFileId();
- $this->assertTrue($last_fid > $last_fid_prior, t('New file got uploaded.'));
- $this->drupalPost(NULL, array(), t('Save'));
- $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), t('Submit handler has correct file info.'));
-
- // Remove, then Submit.
- $this->drupalGet($path . '/' . $last_fid);
- if ($ajax) {
- $this->drupalPostAJAX(NULL, array(), $input_base_name . '_remove_button');
- }
- else {
- $this->drupalPost(NULL, array(), t('Remove'));
- }
- $this->drupalPost(NULL, array(), t('Save'));
- $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), t('Submission after file removal was successful.'));
-
- // Upload, then Remove, then Submit.
- $this->drupalGet($path);
- $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
- if ($ajax) {
- $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
- $this->drupalPostAJAX(NULL, array(), $input_base_name . '_remove_button');
- }
- else {
- $this->drupalPost(NULL, $edit, t('Upload'));
- $this->drupalPost(NULL, array(), t('Remove'));
+ $this->assertTrue($last_fid > $last_fid_prior, t('New file got saved.'));
+ $this->assertRaw(t('The file ids are %fids.', array('%fids' => implode(',', array($last_fid)))), t('Submit handler has correct file info.'));
+
+ // Submit no new input, but with a default file.
+ $this->drupalPost($path . '/' . $last_fid, array(), t('Save'));
+ $this->assertRaw(t('The file ids are %fids.', array('%fids' => implode(',', array($last_fid)))), t('Empty submission did not change an existing file.'));
+
+ // Now, test the Upload and Remove buttons, with and without Ajax.
+ foreach (array(FALSE, TRUE) as $ajax) {
+ // Upload, then Submit.
+ $last_fid_prior = $this->getLastFileId();
+ $this->drupalGet($path);
+ $edit = array($file_field_name => drupal_realpath($test_file->uri));
+ if ($ajax) {
+ $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
+ }
+ else {
+ $this->drupalPost(NULL, $edit, t('Upload'));
+ }
+ $last_fid = $this->getLastFileId();
+ $this->assertTrue($last_fid > $last_fid_prior, t('New file got uploaded.'));
+ $this->drupalPost(NULL, array(), t('Save'));
+ $this->assertRaw(t('The file ids are %fids.', array('%fids' => implode(',', array($last_fid)))), t('Submit handler has correct file info.'));
+
+ // Remove, then Submit.
+ $remove_button_title = $multiple ? t('Remove selected') : t('Remove');
+ $remove_edit = array();
+ if ($multiple) {
+ $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
+ $remove_edit = array($selected_checkbox => '1');
+ }
+ $this->drupalGet($path . '/' . $last_fid);
+ if ($ajax) {
+ $this->drupalPostAJAX(NULL, $remove_edit, $input_base_name . '_remove_button');
+ }
+ else {
+ $this->drupalPost(NULL, $remove_edit, $remove_button_title);
+ }
+ $this->drupalPost(NULL, array(), t('Save'));
+ $this->assertRaw(t('The file ids are %fids.', array('%fids' => '')), t('Submission after file removal was successful.'));
+
+ // Upload, then Remove, then Submit.
+ $this->drupalGet($path);
+ $edit = array($file_field_name => drupal_realpath($test_file->uri));
+ if ($ajax) {
+ $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
+ }
+ else {
+ $this->drupalPost(NULL, $edit, t('Upload'));
+ }
+ $remove_edit = array();
+ if ($multiple) {
+ $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this->getLastFileId() . '][selected]';
+ $remove_edit = array($selected_checkbox => '1');
+ }
+ if ($ajax) {
+ $this->drupalPostAJAX(NULL, $remove_edit, $input_base_name . '_remove_button');
+ }
+ else {
+ $this->drupalPost(NULL, $remove_edit, $remove_button_title);
+ }
+
+ $this->drupalPost(NULL, array(), t('Save'));
+ $this->assertRaw(t('The file ids are %fids.', array('%fids' => '')), t('Submission after file upload and removal was successful.'));
}
- $this->drupalPost(NULL, array(), t('Save'));
- $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), t('Submission after file upload and removal was successful.'));
}
}
}
+
+ // The multiple file upload has additional conditions that need checking.
+ $path = 'file/test/1/1/1';
+ $edit = array('files[nested_file][]' => drupal_realpath($test_file->uri));
+ $fid_list = array();
+
+ $this->drupalGet($path);
+
+ // Add a single file to the upload field.
+ $this->drupalPost(NULL, $edit, t('Upload'));
+ $fid_list[] = $this->getLastFileId();
+ $this->assertFieldByXpath('//input[@name="nested[file][file_' . $fid_list[0] . '][selected]"]', NULL, 'First file successfully uploaded to multiple file element.');
+
+ // Add another file to the same upload field.
+ $this->drupalPost(NULL, $edit, t('Upload'));
+ $fid_list[] = $this->getLastFileId();
+ $this->assertFieldByXpath('//input[@name="nested[file][file_' . $fid_list[1] . '][selected]"]', NULL, 'Second file successfully uploaded to multiple file element.');
+
+ // Save the entire form.
+ $this->drupalPost(NULL, array(), t('Save'));
+ $this->assertRaw(t('The file ids are %fids.', array('%fids' => implode(',', $fid_list))), t('Two files saved into a single multiple file element.'));
+
+ // Delete only the first file.
+ $edit = array(
+ 'nested[file][file_' . $fid_list[0] . '][selected]' => '1',
+ );
+ $this->drupalPost($path . '/' . implode(',', $fid_list), $edit, t('Remove selected'));
+
+ // Check that the first file has been deleted but not the second.
+ $this->assertNoFieldByXpath('//input[@name="nested[file][file_' . $fid_list[0] . '][selected]"]', NULL, 'An individual file can be deleted from a multiple file element.');
+ $this->assertFieldByXpath('//input[@name="nested[file][file_' . $fid_list[1] . '][selected]"]', NULL, 'Second individual file not deleted when the first file is deleted from a multiple file element.');
}
}
diff --git a/core/modules/file/tests/file_module_test.module b/core/modules/file/tests/file_module_test.module
index b962e2a..19096f9 100644
--- a/core/modules/file/tests/file_module_test.module
+++ b/core/modules/file/tests/file_module_test.module
@@ -31,7 +31,7 @@ function file_module_test_menu() {
* @see file_module_test_form_submit()
* @ingroup forms
*/
-function file_module_test_form($form, &$form_state, $tree = TRUE, $extended = FALSE, $default_fid = NULL) {
+function file_module_test_form($form, &$form_state, $tree = TRUE, $extended = TRUE, $multiple = FALSE, $default_fids = NULL) {
$form['#tree'] = (bool) $tree;
$form['nested']['file'] = array(
@@ -41,9 +41,11 @@ function file_module_test_form($form, &$form_state, $tree = TRUE, $extended = FA
'#progress_message' => t('Please wait...'),
'#extended' => (bool) $extended,
'#size' => 13,
+ '#multiple' => (bool) $multiple,
);
- if ($default_fid) {
- $form['nested']['file']['#default_value'] = $extended ? array('fid' => $default_fid) : $default_fid;
+ if ($default_fids) {
+ $default_fids = explode(',', $default_fids);
+ $form['nested']['file']['#default_value'] = $extended ? array('fids' => $default_fids) : $default_fids;
}
$form['textfield'] = array(
@@ -64,12 +66,22 @@ function file_module_test_form($form, &$form_state, $tree = TRUE, $extended = FA
*/
function file_module_test_form_submit($form, &$form_state) {
if ($form['#tree']) {
- $fid = $form['nested']['file']['#extended'] ? $form_state['values']['nested']['file']['fid'] : $form_state['values']['nested']['file'];
+ $uploads = $form_state['values']['nested']['file'];
}
else {
- $fid = $form['nested']['file']['#extended'] ? $form_state['values']['file']['fid'] : $form_state['values']['file'];
+ $uploads = $form_state['values']['file'];
}
- drupal_set_message(t('The file id is %fid.', array('%fid' => $fid)));
+
+ if ($form['nested']['file']['#extended']) {
+ $uploads = $uploads['fids'];
+ }
+
+ $fids = array();
+ foreach ($uploads as $fid) {
+ $fids[] = $fid;
+ }
+
+ drupal_set_message(t('The file ids are %fids.', array('%fids' => implode(',', $fids))));
}
/**
diff --git a/core/modules/file/tests/file_test/file_test.module b/core/modules/file/tests/file_test/file_test.module
index 1d924de..8cff297 100644
--- a/core/modules/file/tests/file_test/file_test.module
+++ b/core/modules/file/tests/file_test/file_test.module
@@ -126,7 +126,7 @@ function _file_test_form_submit(&$form, &$form_state) {
$validators['file_validate_extensions'] = array($form_state['values']['extensions']);
}
- $file = file_save_upload('file_test_upload', $validators, $destination, $form_state['values']['file_test_replace']);
+ $file = file_save_upload('file_test_upload', $validators, $destination, 0, $form_state['values']['file_test_replace']);
if ($file) {
$form_state['values']['file_test_upload'] = $file;
drupal_set_message(t('File @filepath was uploaded.', array('@filepath' => $file->uri)));
diff --git a/core/modules/image/image.field.inc b/core/modules/image/image.field.inc
index 495ca04..a287620 100644
--- a/core/modules/image/image.field.inc
+++ b/core/modules/image/image.field.inc
@@ -18,7 +18,7 @@ function image_field_info() {
'description' => t('This field stores the ID of an image file as an integer value.'),
'settings' => array(
'uri_scheme' => file_default_scheme(),
- 'default_image' => 0,
+ 'default_image' => array(),
'column_groups' => array(
'file' => array(
'label' => t('File'),
@@ -44,7 +44,7 @@ function image_field_info() {
'title_field_required' => 0,
'max_resolution' => '',
'min_resolution' => '',
- 'default_image' => 0,
+ 'default_image' => array(),
),
'default_widget' => 'image_image',
'default_formatter' => 'image',
@@ -231,7 +231,7 @@ function image_field_prepare_view($entity_type, $entities, $field, $instances, $
// If there are no files specified at all, use the default.
foreach ($entities as $id => $entity) {
if (empty($items[$id])) {
- $fid = 0;
+ $fid = array();
// Use the default for the instance if one is available.
if (!empty($instances[$id]['settings']['default_image'])) {
$fid = $instances[$id]['settings']['default_image'];
@@ -242,7 +242,7 @@ function image_field_prepare_view($entity_type, $entities, $field, $instances, $
}
// Add the default image if one is found.
- if ($fid && ($file = file_load($fid))) {
+ if ($fid && ($file = file_load($fid[0]))) {
$items[$id][0] = (array) $file + array(
'is_default' => TRUE,
'alt' => '',
@@ -313,16 +313,17 @@ function image_field_is_empty($item, $field) {
*/
function image_field_widget_process($element, &$form_state, $form) {
$item = $element['#value'];
- $item['fid'] = $element['fid']['#value'];
+ $item['fids'] = $element['fids']['#value'];
$element['#theme'] = 'image_widget';
$element['#attached']['css'][] = drupal_get_path('module', 'image') . '/image.theme.css';
// Add the image preview.
- if ($element['#file'] && $element['#preview_image_style']) {
+ if (!empty($element['#files']) && $element['#preview_image_style']) {
+ $file = reset($element['#files']);
$variables = array(
'style_name' => $element['#preview_image_style'],
- 'uri' => $element['#file']->uri,
+ 'uri' => $file->uri,
);
// Determine image dimensions.
@@ -331,7 +332,7 @@ function image_field_widget_process($element, &$form_state, $form) {
$variables['height'] = $element['#value']['height'];
}
else {
- $info = image_get_info($element['#file']->uri);
+ $info = image_get_info($file->uri);
if (is_array($info)) {
$variables['width'] = $info['width'];
@@ -372,7 +373,7 @@ function image_field_widget_process($element, &$form_state, $form) {
// @see http://www.gawds.org/show.php?contentid=28
'#maxlength' => 512,
'#weight' => -2,
- '#access' => (bool) $item['fid'] && $element['#alt_field'],
+ '#access' => (bool) $item['fids'] && $element['#alt_field'],
'#element_validate' => $settings['alt_field_required'] == 1 ? array('_image_field_required_fields_validate') : array(),
);
$element['title'] = array(
@@ -382,7 +383,7 @@ function image_field_widget_process($element, &$form_state, $form) {
'#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'),
'#maxlength' => 1024,
'#weight' => -1,
- '#access' => (bool) $item['fid'] && $element['#title_field'],
+ '#access' => (bool) $item['fids'] && $element['#title_field'],
'#element_validate' => $settings['alt_field_required'] == 1 ? array('_image_field_required_fields_validate') : array(),
);
@@ -437,8 +438,9 @@ function theme_image_widget($variables) {
}
$output .= '<div class="image-widget-data">';
- if ($element['fid']['#value'] != 0) {
- $element['filename']['#markup'] .= ' <span class="file-size">(' . format_size($element['#file']->filesize) . ')</span> ';
+ if (!empty($element['fids']['#value'])) {
+ $file = reset($element['#files']);
+ $element['file_' . $file->fid]['filename']['#markup'] .= ' <span class="file-size">(' . format_size($file->filesize) . ')</span> ';
}
$output .= drupal_render_children($element);
$output .= '</div>';
diff --git a/core/modules/image/image.module b/core/modules/image/image.module
index 9e07bb5..f95c633 100644
--- a/core/modules/image/image.module
+++ b/core/modules/image/image.module
@@ -349,8 +349,8 @@ function image_field_delete_field($field) {
}
// The value of a managed_file element can be an array if #extended == TRUE.
- $fid = (is_array($field['settings']['default_image']) ? $field['settings']['default_image']['fid'] : $field['settings']['default_image']);
- if ($fid && ($file = file_load($fid))) {
+ $fid = (isset($field['settings']['default_image']['fids']) ? $field['settings']['default_image']['fids'] : $field['settings']['default_image']);
+ if ($fid && ($file = file_load($fid[0]))) {
file_usage()->delete($file, 'image', 'default_image', $field['id']);
}
}
@@ -364,10 +364,10 @@ function image_field_update_field($field, $prior_field, $has_data) {
}
// The value of a managed_file element can be an array if #extended == TRUE.
- $fid_new = (is_array($field['settings']['default_image']) ? $field['settings']['default_image']['fid'] : $field['settings']['default_image']);
- $fid_old = (is_array($prior_field['settings']['default_image']) ? $prior_field['settings']['default_image']['fid'] : $prior_field['settings']['default_image']);
+ $fid_new = (isset($field['settings']['default_image']['fids']) ? $field['settings']['default_image']['fids'] : $field['settings']['default_image']);
+ $fid_old = (isset($prior_field['settings']['default_image']['fids']) ? $prior_field['settings']['default_image']['fids'] : $prior_field['settings']['default_image']);
- $file_new = $fid_new ? file_load($fid_new) : FALSE;
+ $file_new = $fid_new ? file_load($fid_new[0]) : FALSE;
if ($fid_new != $fid_old) {
@@ -379,7 +379,7 @@ function image_field_update_field($field, $prior_field, $has_data) {
}
// Is there an old file?
- if ($fid_old && ($file_old = file_load($fid_old))) {
+ if ($fid_old && ($file_old = file_load($fid_old[0]))) {
file_usage()->delete($file_old, 'image', 'default_image', $field['uuid']);
}
}
@@ -428,16 +428,16 @@ function image_field_update_instance($instance, $prior_instance) {
// The value of a managed_file element can be an array if the #extended
// property is set to TRUE.
$fid_new = $instance['settings']['default_image'];
- if (is_array($fid_new)) {
- $fid_new = $fid_new['fid'];
+ if (isset($fid_new['fids'])) {
+ $fid_new = $fid_new['fids'];
}
$fid_old = $prior_instance['settings']['default_image'];
- if (is_array($fid_old)) {
- $fid_old = $fid_old['fid'];
+ if (isset($fid_old['fids'])) {
+ $fid_old = $fid_old['fids'];
}
// If the old and new files do not match, update the default accordingly.
- $file_new = $fid_new ? file_load($fid_new) : FALSE;
+ $file_new = $fid_new ? file_load($fid_new[0]) : FALSE;
if ($fid_new != $fid_old) {
// Save the new file, if present.
if ($file_new) {
@@ -446,7 +446,7 @@ function image_field_update_instance($instance, $prior_instance) {
file_usage()->add($file_new, 'image', 'default_image', $instance['uuid']);
}
// Delete the old file, if present.
- if ($fid_old && ($file_old = file_load($fid_old))) {
+ if ($fid_old && ($file_old = file_load($fid_old[0]))) {
file_usage()->delete($file_old, 'image', 'default_image', $instance['uuid']);
}
}
@@ -1029,4 +1029,3 @@ function image_filter_keyword($value, $current_pixels, $new_pixels) {
function _image_effect_definitions_sort($a, $b) {
return strcasecmp($a['name'], $b['name']);
}
-
diff --git a/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php b/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php
index 6d2083a..8062ff6 100644
--- a/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php
+++ b/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php
@@ -61,12 +61,12 @@ class ImageWidget extends FileWidget {
if ($this->field['cardinality'] == 1) {
// If there's only one field, return it as delta 0.
- if (empty($elements[0]['#default_value']['fid'])) {
- $elements[0]['#description'] = theme('file_upload_help', array('description' => $this->instance['description'], 'upload_validators' => $elements[0]['#upload_validators']));
+ if (empty($elements[0]['#default_value']['fids'])) {
+ $elements[0]['#description'] = theme('file_upload_help', array('description' => $this->instance['description'], 'upload_validators' => $elements[0]['#upload_validators'], 'cardinality' => $this->field['cardinality']));
}
}
else {
- $elements['#file_upload_description'] = theme('file_upload_help', array('upload_validators' => $elements[0]['#upload_validators']));
+ $elements['#file_upload_description'] = theme('file_upload_help', array('upload_validators' => $elements[0]['#upload_validators'], 'cardinality' => $this->field['cardinality']));
}
return $elements;
diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php
index 01fa5f4..a30b7ed 100644
--- a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php
+++ b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php
@@ -43,10 +43,10 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
// Create an image field and add an instance to the article content type.
$field_name = strtolower($this->randomName());
$field_settings = array(
- 'default_image' => $default_images['field']->fid,
+ 'default_image' => array($default_images['field']->fid),
);
$instance_settings = array(
- 'default_image' => $default_images['instance']->fid,
+ 'default_image' => array($default_images['instance']->fid),
);
$widget_settings = array(
'preview_image_style' => 'medium',
@@ -63,7 +63,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
'label' => $instance['label'],
'required' => $instance['required'],
'settings' => array(
- 'default_image' => $default_images['instance2']->fid,
+ 'default_image' => array($default_images['instance2']->fid),
),
'widget' => $instance['widget'],
);
@@ -76,7 +76,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
// Confirm the defaults are present on the article field settings form.
$this->drupalGet("admin/structure/types/manage/article/fields/$field_name/field-settings");
$this->assertFieldByXpath(
- '//input[@name="field[settings][default_image][fid]"]',
+ '//input[@name="field[settings][default_image][fids]"]',
$default_images['field']->fid,
format_string(
'Article image field default equals expected file ID of @fid.',
@@ -86,7 +86,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
// Confirm the defaults are present on the article field edit form.
$this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
$this->assertFieldByXpath(
- '//input[@name="instance[settings][default_image][fid]"]',
+ '//input[@name="instance[settings][default_image][fids]"]',
$default_images['instance']->fid,
format_string(
'Article image field instance default equals expected file ID of @fid.',
@@ -97,7 +97,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
// Confirm the defaults are present on the page field settings form.
$this->drupalGet("admin/structure/types/manage/page/fields/$field_name/field-settings");
$this->assertFieldByXpath(
- '//input[@name="field[settings][default_image][fid]"]',
+ '//input[@name="field[settings][default_image][fids]"]',
$default_images['field']->fid,
format_string(
'Page image field default equals expected file ID of @fid.',
@@ -107,7 +107,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
// Confirm the defaults are present on the page field edit form.
$this->drupalGet("admin/structure/types/manage/page/fields/$field_name");
$this->assertFieldByXpath(
- '//input[@name="instance[settings][default_image][fid]"]',
+ '//input[@name="instance[settings][default_image][fids]"]',
$default_images['instance2']->fid,
format_string(
'Page image field instance default equals expected file ID of @fid.',
@@ -140,13 +140,13 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
);
// Upload a new default for the field.
- $field['settings']['default_image'] = $default_images['field_new']->fid;
+ $field['settings']['default_image'] = array($default_images['field_new']->fid);
field_update_field($field);
// Confirm that the new default is used on the article field settings form.
$this->drupalGet("admin/structure/types/manage/article/fields/$field_name/field-settings");
$this->assertFieldByXpath(
- '//input[@name="field[settings][default_image][fid]"]',
+ '//input[@name="field[settings][default_image][fids]"]',
$default_images['field_new']->fid,
format_string(
'Updated image field default equals expected file ID of @fid.',
@@ -175,14 +175,14 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
);
// Upload a new default for the article's field instance.
- $instance['settings']['default_image'] = $default_images['instance_new']->fid;
+ $instance['settings']['default_image'] = array($default_images['instance_new']->fid);
field_update_instance($instance);
// Confirm the new field instance default is used on the article field
// admin form.
$this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
$this->assertFieldByXpath(
- '//input[@name="instance[settings][default_image][fid]"]',
+ '//input[@name="instance[settings][default_image][fids]"]',
$default_images['instance_new']->fid,
format_string(
'Updated article image field instance default equals expected file ID of @fid.',
@@ -220,7 +220,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
// Confirm the article field instance default has been removed.
$this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
$this->assertFieldByXpath(
- '//input[@name="instance[settings][default_image][fid]"]',
+ '//input[@name="instance[settings][default_image][fids]"]',
'',
'Updated article image field instance default has been successfully removed.'
);
diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php
index 7c42a5b..b91d9af 100644
--- a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php
+++ b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php
@@ -233,7 +233,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Clear field info cache so the new default image is detected.
field_info_cache_clear();
$field = field_info_field($field_name);
- $image = file_load($field['settings']['default_image']);
+ $image = file_load($field['settings']['default_image'][0]);
$this->assertTrue($image->status == FILE_STATUS_PERMANENT, 'The default image status is permanent.');
$default_output = theme('image', array('uri' => $image->uri));
$this->drupalGet('node/' . $node->nid);
@@ -255,7 +255,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Remove default image from the field and make sure it is no longer used.
$edit = array(
- 'field[settings][default_image][fid]' => 0,
+ 'field[settings][default_image][fids]' => 0,
);
$this->drupalPost("admin/structure/types/manage/article/fields/$field_name/field-settings", $edit, t('Save field settings'));
// Clear field info cache so the new default image is detected.
@@ -275,7 +275,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
field_info_cache_clear();
$private_field = field_info_field($private_field_name);
- $image = file_load($private_field['settings']['default_image']);
+ $image = file_load($private_field['settings']['default_image'][0]);
$this->assertEqual('private', file_uri_scheme($image->uri), 'Default image uses private:// scheme.');
$this->assertTrue($image->status == FILE_STATUS_PERMANENT, 'The default image status is permanent.');
// Create a new node with no image attached and ensure that default private
diff --git a/core/modules/locale/locale.bulk.inc b/core/modules/locale/locale.bulk.inc
index 6a452d8..49273d8 100644
--- a/core/modules/locale/locale.bulk.inc
+++ b/core/modules/locale/locale.bulk.inc
@@ -108,7 +108,7 @@ function locale_translate_import_form($form, &$form_state) {
*/
function locale_translate_import_form_submit($form, &$form_state) {
// Ensure we have the file uploaded.
- if ($file = file_save_upload('file', $form['file']['#upload_validators'], 'translations://')) {
+ if ($file = file_save_upload('file', $form['file']['#upload_validators'], 'translations://', 0)) {
// Add language, if not yet supported.
$language = language_load($form_state['values']['langcode']);
diff --git a/core/modules/node/node.api.php b/core/modules/node/node.api.php
index 5854170..78a3139 100644
--- a/core/modules/node/node.api.php
+++ b/core/modules/node/node.api.php
@@ -1123,7 +1123,7 @@ function hook_delete(\Drupal\Core\Entity\EntityInterface $node) {
*/
function hook_prepare(\Drupal\Core\Entity\EntityInterface $node) {
if ($file = file_check_upload($field_name)) {
- $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE));
+ $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE), FALSE, 0);
if ($file) {
if (!image_get_info($file->uri)) {
form_set_error($field_name, t('Uploaded file is not a valid image'));
diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php
index 23eb3da..cb68737 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php
@@ -40,8 +40,8 @@ class UserPictureUpgradePathTest extends UpgradePathTestBase {
// Retrieve the field instance and check for migrated settings.
$instance = field_info_instance('user', 'user_picture', 'user');
- $file = entity_load('file', $instance['settings']['default_image']);
- $this->assertIdentical($instance['settings']['default_image'], $file->id(), 'Default user picture has been migrated.');
+ $file = entity_load('file', $instance['settings']['default_image'][0]);
+ $this->assertIdentical($instance['settings']['default_image'][0], $file->id(), 'Default user picture has been migrated.');
$this->assertEqual($file->uri, 'public://user_pictures_dir/druplicon.png', 'File id matches the uri expected.');
$this->assertEqual($file->filename, 'druplicon.png');
$this->assertEqual($file->langcode, LANGUAGE_NOT_SPECIFIED);
diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc
index fde1bcc..e0be14f 100644
--- a/core/modules/system/system.admin.inc
+++ b/core/modules/system/system.admin.inc
@@ -619,7 +619,7 @@ function system_theme_settings_validate($form, &$form_state) {
$validators = array('file_validate_is_image' => array());
// Check for a new uploaded logo.
- $file = file_save_upload('logo_upload', $validators);
+ $file = file_save_upload('logo_upload', $validators, FALSE, 0);
if (isset($file)) {
// File upload was attempted.
if ($file) {
@@ -635,7 +635,7 @@ function system_theme_settings_validate($form, &$form_state) {
$validators = array('file_validate_extensions' => array('ico png gif jpg jpeg apng svg'));
// Check for a new uploaded favicon.
- $file = file_save_upload('favicon_upload', $validators);
+ $file = file_save_upload('favicon_upload', $validators, FALSE, 0);
if (isset($file)) {
// File upload was attempted.
if ($file) {
diff --git a/core/modules/system/system.module b/core/modules/system/system.module
index bcc6bee..4cae037 100644
--- a/core/modules/system/system.module
+++ b/core/modules/system/system.module
@@ -499,6 +499,8 @@ function system_element_info() {
);
$types['file'] = array(
'#input' => TRUE,
+ '#multiple' => FALSE,
+ '#process' => array('form_process_file'),
'#size' => 60,
'#pre_render' => array('form_pre_render_file'),
'#theme' => 'input__file',
diff --git a/core/modules/update/update.manager.inc b/core/modules/update/update.manager.inc
index 0047a52..bc84058 100644
--- a/core/modules/update/update.manager.inc
+++ b/core/modules/update/update.manager.inc
@@ -645,7 +645,7 @@ function update_manager_install_form_submit($form, &$form_state) {
elseif ($_FILES['files']['name']['project_upload']) {
$validators = array('file_validate_extensions' => array(archiver_get_extensions()));
$field = 'project_upload';
- if (!($finfo = file_save_upload($field, $validators, NULL, FILE_EXISTS_REPLACE))) {
+ if (!($finfo = file_save_upload($field, $validators, NULL, 0, FILE_EXISTS_REPLACE))) {
// Failed to upload the file. file_save_upload() calls form_set_error() on
// failure.
return;