diff --git a/core/modules/file/src/Element/ManagedFile.php b/core/modules/file/src/Element/ManagedFile.php index 8f2f1d2..07c2ef5 100644 --- a/core/modules/file/src/Element/ManagedFile.php +++ b/core/modules/file/src/Element/ManagedFile.php @@ -305,6 +305,10 @@ public static function processManagedFile(&$element, FormStateInterface $form_st } } + if (isset($element['#upload_validators']['file_validate_extensions'][0])) { + $element['upload']['#attributes']['data-drupal-validate-extensions'] = $element['#upload_validators']['file_validate_extensions'][0]; + } + // Let #id point to the file element, so the field label's 'for' corresponds // with it. $element['#id'] = &$element['upload']['#id']; diff --git a/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php index 16cdf83..1f11125 100644 --- a/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php +++ b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php @@ -236,7 +236,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#type' => 'managed_file', '#upload_location' => $items[$delta]->getUploadLocation(), '#upload_validators' => $items[$delta]->getUploadValidators(), - '#after_build' => array(array($this, 'addValidateDataForJs')), '#value_callback' => array(get_class($this), 'value'), '#process' => array_merge($element_info['#process'], array(array(get_class($this), 'process'))), '#progress_indicator' => $this->getSetting('progress_indicator'), @@ -279,26 +278,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen } /** - * Form API callback. Attaches data- attributes to the file input field for - * JavaScript's benefit. - * - * @param array $element - * The FileWidget's render array. - * @param FormStateInterface $form_state - * The form state. - * - * @return array - * The updated render array. - */ - public function addValidateDataForJs(array $element, FormStateInterface &$form_state) { - // File extensions go on the ManagedFile's 'upload' child as data attribute. - if (is_array($element['upload']) && !empty($element['#upload_validators']['file_validate_extensions'])) { - $element['upload']['#attributes']['data-drupal-validate-extensions'] = $element['#upload_validators']['file_validate_extensions'][0]; - } - return $element; - } - - /** * {@inheritdoc} */ public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { diff --git a/core/modules/file/src/Tests/FileManagedFileElementTest.php b/core/modules/file/src/Tests/FileManagedFileElementTest.php index e1528bc..a58c846 100644 --- a/core/modules/file/src/Tests/FileManagedFileElementTest.php +++ b/core/modules/file/src/Tests/FileManagedFileElementTest.php @@ -23,6 +23,10 @@ function testManagedFile() { $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.'); + // Check that relevant data- attributes are present for validations + $this->drupalGet('file/test/validation/extension'); + $this->assertFieldByXPath('//input[@name="files[file]" and @data-drupal-validate-extensions="txt"]', NULL, 'The data-drupal-validate-extensions attribute is present on the upload element.'); + // Perform the tests with all permutations of $form['#tree'], // $element['#extended'], and $element['#multiple']. $test_file = $this->getTestFile('text'); diff --git a/core/modules/file/tests/file_module_test/file_module_test.routing.yml b/core/modules/file/tests/file_module_test/file_module_test.routing.yml index 15f7d0f..0c85f6e 100644 --- a/core/modules/file/tests/file_module_test/file_module_test.routing.yml +++ b/core/modules/file/tests/file_module_test/file_module_test.routing.yml @@ -8,3 +8,11 @@ file_module_test.managed_test: default_fids: NULL requirements: _access: 'TRUE' + +file_module_test.managed_validations_test: + path: '/file/test/validation/{validation_type}' + defaults: + _form: '\Drupal\file_module_test\Form\FileModuleTestValidationForm' + validation_type: 'extension' + requirements: + _access: 'TRUE' diff --git a/core/modules/file/tests/file_module_test/src/Form/FileModuleTestValidationForm.php b/core/modules/file/tests/file_module_test/src/Form/FileModuleTestValidationForm.php new file mode 100644 index 0000000..9674b09 --- /dev/null +++ b/core/modules/file/tests/file_module_test/src/Form/FileModuleTestValidationForm.php @@ -0,0 +1,70 @@ + ['txt']]; + break; + case 'size': + $validator = ['file_validate_size' => [1024]]; + break; + default: + return FALSE; + } + + $form['file'] = array( + '#type' => 'managed_file', + '#title' => $this->t('Managed File'), + '#upload_location' => 'public://test', + '#progress_message' => $this->t('Please wait...'), + '#upload_validators' => $validator, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Save'), + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + drupal_set_message($this->t('Test submission handler was allowed to execute.')); + } + +}