Skip to content
Snippets Groups Projects
Verified Commit e3930ea5 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #59750 by quietone, larowlan, mikeryan, quicksketch, sreynen, kleinmp,...

Issue #59750 by quietone, larowlan, mikeryan, quicksketch, sreynen, kleinmp, kim.pepper, Bèr Kessels, chx, S3b0uN3t, ravi.shankar, mrddthi, webchick, neclimdul, Lendude: Required flag on file forms breaks on validation

(cherry picked from commit a5b44e8f)
parent bcf1477d
No related branches found
No related tags found
24 merge requests!8394[warning] array_flip(): Can only flip STRING and INTEGER values, when saving a non-revisionable custom content entity,!7780issue 3443822: fix for 'No route found for the specified format html. Supported formats: json, xml.',!5013Issue #3071143: Table Render Array Example Is Incorrect,!4848Issue #1566662: Update module should send notifications on Thursdays,!4792Issue #2230689: Remove redundant "Italic" style,!4220Issue #3368223: Link field > Access to internal links is not checked on display.,!3884Issue #3356842,!3870Issue #3087868,!3812Draft: Issue #3339373 by alexpott, andypost, mondrake:...,!3686Issue #3219967 against 9.5.x,!3683Issue #2939397: Clearing AliasManager cache with root path raises warning,!3543Issue #3344259: Allow ajax dialog to have focus configurable,!3356Issue #3209129: Scrolling problems when adding a block via layout builder,!2921Issue #1383696: Allow a custom HTML element to be selected for a grouping field,!2857Issue #3314541: Remove unnecessary fill from SVG icon for the "Media Library" CKEditor 5 button — enabling dark mode support in contrib,!2280Issue #3280415: Metapackage Generator Breaks Under Composer --no-dev,!2205Quote all names in the regions section.,!2050Issue #3272969: Remove UnqiueField constraint.,!1956Issue #3268872: hook_views_invalidate_cache not called when a view is deleted,!1893Issue #3217260: Add a way to make media captions not editable in CKEditor,!1459Issue #3087632: menu_name max length is too long,!878Issue #3221534: throw an exception when IDs passed to loadMultiple() are badly formed,!866Issue #2845319: The highlighting of the 'Home' menu-link does not respect query strings and fragment identifiers,!204Issue #3040556: It is not possible to react to an entity being duplicated
......@@ -15,6 +15,10 @@
* - #multiple: A Boolean indicating whether multiple files may be uploaded.
* - #size: The size of the file input element in characters.
*
* The value of this form element will always be an array of
* \Symfony\Component\HttpFoundation\File\UploadedFile objects, regardless of
* whether #multiple is TRUE or FALSE
*
* @FormElement("file")
*/
class File extends FormElement {
......@@ -36,6 +40,9 @@ public function getInfo() {
],
'#theme' => 'input__file',
'#theme_wrappers' => ['form_element'],
'#value_callback' => [
[$class, 'valueCallback'],
],
];
}
......@@ -72,4 +79,23 @@ public static function preRenderFile($element) {
return $element;
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input === FALSE) {
return NULL;
}
$parents = $element['#parents'];
$element_name = array_shift($parents);
$uploaded_files = \Drupal::request()->files->get('files', []);
$uploaded_file = $uploaded_files[$element_name] ?? NULL;
if ($uploaded_file) {
// Cast this to an array so that the structure is consistent regardless of
// whether #value is set or not.
return (array) $uploaded_file;
}
return NULL;
}
}
......@@ -10,3 +10,9 @@ file.save_upload_from_form_test:
_form: '\Drupal\file_test\Form\FileTestSaveUploadFromForm'
requirements:
_access: 'TRUE'
file.required_test:
path: '/file-test/upload_required'
defaults:
_form: '\Drupal\file_test\Form\FileRequiredTestForm'
requirements:
_access: 'TRUE'
<?php
namespace Drupal\file_test\Form;
use Drupal\Core\Form\FormStateInterface;
/**
* File required test form class.
*/
class FileRequiredTestForm extends FileTestForm {
/**
* {@inheritdoc}
*/
public function getFormId() {
return '_file_required_test_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['file_test_upload']['#required'] = TRUE;
return $form;
}
}
......@@ -732,4 +732,26 @@ public function testInvalidUtf8FilenameUpload() {
$this->assertFileDoesNotExist('temporary://' . $filename);
}
/**
* Tests the file_save_upload() function when the field is required.
*/
public function testRequired() {
// Reset the hook counters to get rid of the 'load' we just called.
file_test_reset();
// Confirm the field is required.
$this->drupalGet('file-test/upload_required');
$this->submitForm([], 'Submit');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->responseContains('field is required');
// Confirm that uploading another file works.
$image = current($this->drupalGetTestFiles('image'));
$edit = ['files[file_test_upload]' => \Drupal::service('file_system')->realpath($image->uri)];
$this->drupalGet('file-test/upload_required');
$this->submitForm($edit, 'Submit');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->responseContains('You WIN!');
}
}
......@@ -235,7 +235,6 @@ public function buildForm(array $form, FormStateInterface $form_state, $theme =
$form['logo']['settings']['logo_upload'] = [
'#type' => 'file',
'#title' => $this->t('Upload logo image'),
'#maxlength' => 40,
'#description' => $this->t("If you don't have direct file access to the server, use this field to upload your logo."),
'#upload_validators' => [
'file_validate_is_image' => [],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment