diff --git a/core/modules/file/src/Element/ManagedFile.php b/core/modules/file/src/Element/ManagedFile.php index c2fa103cb380ad03824416fe6522ac0d60400026..1378ff358946fcdff881f4b5f5e177cbba09d809 100644 --- a/core/modules/file/src/Element/ManagedFile.php +++ b/core/modules/file/src/Element/ManagedFile.php @@ -372,19 +372,25 @@ public static function validateManagedFile(&$element, FormStateInterface $form_s if ($file->isPermanent()) { $references = static::fileUsage()->listUsage($file); if (empty($references)) { - $form_state->setError($element, t('The file used in the !name field may not be referenced.', ['!name' => $element['#title']])); + // We expect the field name placeholder value to be wrapped in t() + // here, so it won't be escaped again as it's already marked safe. + $form_state->setError($element, t('The file used in the @name field may not be referenced.', ['@name' => $element['#title']])); } } } else { - $form_state->setError($element, t('The file referenced by the !name field does not exist.', ['!name' => $element['#title']])); + // We expect the field name placeholder value to be wrapped in t() + // here, so it won't be escaped again as it's already marked safe. + $form_state->setError($element, t('The file referenced by the @name field does not exist.', ['@name' => $element['#title']])); } } } // Check required property based on the FID. if ($element['#required'] && empty($element['fids']['#value']) && !in_array($clicked_button, ['upload_button', 'remove_button'])) { - $form_state->setError($element, t('!name is required.', ['!name' => $element['#title']])); + // We expect the field name placeholder value to be wrapped in t() + // here, so it won't be escaped again as it's already marked safe. + $form_state->setError($element, t('@name is required.', ['@name' => $element['#title']])); } // Consolidate the array value of this field to array of FIDs. diff --git a/core/modules/file/src/Tests/FileManagedFileElementTest.php b/core/modules/file/src/Tests/FileManagedFileElementTest.php index 9953217162d30fb4335bd4a335e5ec8333f83869..e1528bcc6ec88dab0342bb240217ebdeb4e887f9 100644 --- a/core/modules/file/src/Tests/FileManagedFileElementTest.php +++ b/core/modules/file/src/Tests/FileManagedFileElementTest.php @@ -154,4 +154,26 @@ function testManagedFile() { $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.'); } + + /** + * Ensure that warning is shown if file on the field has been removed. + */ + public function testManagedFileRemoved() { + $this->drupalGet('file/test/1/0/1'); + $test_file = $this->getTestFile('text'); + $file_field_name = 'files[nested_file][]'; + + $edit = [$file_field_name => drupal_realpath($test_file->getFileUri())]; + $this->drupalPostForm(NULL, $edit, t('Upload')); + + $fid = $this->getLastFileId(); + $file = \Drupal::entityManager()->getStorage('file')->load($fid); + $file->delete(); + + $this->drupalPostForm(NULL, $edit, t('Upload')); + // We expect the title 'Managed <em>file & butter</em>' which got escaped + // via a t() call before. + $this->assertRaw('The file referenced by the Managed <em>file & butter</em> field does not exist.'); + } + } diff --git a/core/modules/file/tests/file_module_test/src/Form/FileModuleTestForm.php b/core/modules/file/tests/file_module_test/src/Form/FileModuleTestForm.php index 052909045917280e08e80cc2bf38cddb986e0b31..bb44a6ea4583bcbed77e339d99a29fd4fe48ace9 100644 --- a/core/modules/file/tests/file_module_test/src/Form/FileModuleTestForm.php +++ b/core/modules/file/tests/file_module_test/src/Form/FileModuleTestForm.php @@ -43,7 +43,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $tree = T $form['nested']['file'] = array( '#type' => 'managed_file', - '#title' => $this->t('Managed file'), + '#title' => $this->t('Managed <em>@type</em>', ['@type' => 'file & butter']), '#upload_location' => 'public://test', '#progress_message' => $this->t('Please wait...'), '#extended' => (bool) $extended,