From cead42ce15e3cc21dea4bfce910e6b140eaaa320 Mon Sep 17 00:00:00 2001
From: xjm <xjm@65776.no-reply.drupal.org>
Date: Fri, 18 Sep 2015 15:27:03 -0400
Subject: [PATCH] Issue #2569289 by lauriii, dawehner, stefan.r: Replace
 remaining !placeholder for Non-URL HTML outputs only in #title in file
 widgets

---
 core/modules/file/src/Element/ManagedFile.php | 12 +++++++---
 .../src/Tests/FileManagedFileElementTest.php  | 22 +++++++++++++++++++
 .../src/Form/FileModuleTestForm.php           |  2 +-
 3 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/core/modules/file/src/Element/ManagedFile.php b/core/modules/file/src/Element/ManagedFile.php
index c2fa103cb380..1378ff358946 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 9953217162d3..e1528bcc6ec8 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 &amp; 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 052909045917..bb44a6ea4583 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,
-- 
GitLab