From e814d6fba8773915a0676553aad419651d988f94 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Mon, 22 Jan 2018 15:28:16 +0000
Subject: [PATCH] Issue #2935254 by kim.pepper: Remove all usages of
 drupal_get_message and drupal_set_message in core includes

---
 core/authorize.php             |  2 +-
 core/includes/batch.inc        |  2 +-
 core/includes/errors.inc       |  2 +-
 core/includes/file.inc         | 19 ++++++++++---------
 core/includes/form.inc         |  2 +-
 core/includes/install.core.inc |  2 +-
 core/includes/install.inc      |  2 +-
 core/rebuild.php               |  2 +-
 8 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/core/authorize.php b/core/authorize.php
index a72e0ac68721..9ab60dcdcb72 100644
--- a/core/authorize.php
+++ b/core/authorize.php
@@ -113,7 +113,7 @@ function authorize_access_allowed(Request $request) {
       $page_title = $results['page_title'];
     }
     if (!empty($results['page_message'])) {
-      drupal_set_message($results['page_message']['message'], $results['page_message']['type']);
+      \Drupal::messenger()->addMessage($results['page_message']['message'], $results['page_message']['type']);
     }
 
     $content['authorize_report'] = [
diff --git a/core/includes/batch.inc b/core/includes/batch.inc
index 402f195c2f19..de43364d6873 100644
--- a/core/includes/batch.inc
+++ b/core/includes/batch.inc
@@ -42,7 +42,7 @@ function _batch_page(Request $request) {
   if (!$batch) {
     $batch = \Drupal::service('batch.storage')->load($request_id);
     if (!$batch) {
-      drupal_set_message(t('No active batch.'), 'error');
+      \Drupal::messenger()->addError(t('No active batch.'));
       return new RedirectResponse(\Drupal::url('<front>', [], ['absolute' => TRUE]));
     }
   }
diff --git a/core/includes/errors.inc b/core/includes/errors.inc
index e3fd2256c138..e4f9f264ef7d 100644
--- a/core/includes/errors.inc
+++ b/core/includes/errors.inc
@@ -272,7 +272,7 @@ function _drupal_log_error($error, $fatal = FALSE) {
     if ($message) {
       if (\Drupal::hasService('session')) {
         // Message display is dependent on sessions being available.
-        drupal_set_message($message, $class, TRUE);
+        \Drupal::messenger()->addMessage($message, $class, TRUE);
       }
       else {
         print $message;
diff --git a/core/includes/file.inc b/core/includes/file.inc
index c338d5ab563d..3273e7e2ff35 100644
--- a/core/includes/file.inc
+++ b/core/includes/file.inc
@@ -512,8 +512,9 @@ function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_E
 
   // Assert that the source file actually exists.
   if (!file_exists($source)) {
-    // @todo Replace drupal_set_message() calls with exceptions instead.
-    drupal_set_message(t('The specified file %file could not be moved/copied because no file by that name exists. Please check that you supplied the correct filename.', ['%file' => $original_source]), 'error');
+    // @todo Replace \Drupal::messenger()->addError() calls with exceptions
+    // instead.
+    \Drupal::messenger()->addError(t('The specified file %file could not be moved/copied because no file by that name exists. Please check that you supplied the correct filename.', ['%file' => $original_source]));
     if (($realpath = $file_system->realpath($original_source)) !== FALSE) {
       $logger->notice('File %file (%realpath) could not be moved/copied because it does not exist.', ['%file' => $original_source, '%realpath' => $realpath]);
     }
@@ -539,7 +540,7 @@ function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_E
     if (!file_prepare_directory($dirname)) {
       // The destination is not valid.
       $logger->notice('File %file could not be moved/copied because the destination directory %destination is not configured correctly.', ['%file' => $original_source, '%destination' => $dirname]);
-      drupal_set_message(t('The specified file %file could not be moved/copied because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', ['%file' => $original_source]), 'error');
+      \Drupal::messenger()->addError(t('The specified file %file could not be moved/copied because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', ['%file' => $original_source]));
       return FALSE;
     }
   }
@@ -547,7 +548,7 @@ function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_E
   // Determine whether we can perform this operation based on overwrite rules.
   $destination = file_destination($destination, $replace);
   if ($destination === FALSE) {
-    drupal_set_message(t('The file %file could not be moved/copied because a file by that name already exists in the destination directory.', ['%file' => $original_source]), 'error');
+    \Drupal::messenger()->addError(t('The file %file could not be moved/copied because a file by that name already exists in the destination directory.', ['%file' => $original_source]));
     $logger->notice('File %file could not be moved/copied because a file by that name already exists in the destination directory (%destination)', ['%file' => $original_source, '%destination' => $destination]);
     return FALSE;
   }
@@ -556,7 +557,7 @@ function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_E
   $real_source = $file_system->realpath($source);
   $real_destination = $file_system->realpath($destination);
   if ($source == $destination || ($real_source !== FALSE) && ($real_source == $real_destination)) {
-    drupal_set_message(t('The specified file %file was not moved/copied because it would overwrite itself.', ['%file' => $source]), 'error');
+    \Drupal::messenger()->addError(t('The specified file %file was not moved/copied because it would overwrite itself.', ['%file' => $source]));
     $logger->notice('File %file could not be moved/copied because it would overwrite itself.', ['%file' => $source]);
     return FALSE;
   }
@@ -698,8 +699,8 @@ function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXIST
  * @param $extensions
  *   A space-separated list of extensions that should not be altered.
  * @param $alerts
- *   If TRUE, drupal_set_message() will be called to display a message if the
- *   file name was changed.
+ *   If TRUE, \Drupal::messenger()->addStatus() will be called to display
+ *   a message if the file name was changed.
  *
  * @return string
  *   The potentially modified $filename.
@@ -735,7 +736,7 @@ function file_munge_filename($filename, $extensions, $alerts = TRUE) {
     $filename = $new_filename . '.' . $final_extension;
 
     if ($alerts && $original != $filename) {
-      drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $filename]));
+      \Drupal::messenger()->addStatus(t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $filename]));
     }
   }
 
@@ -969,7 +970,7 @@ function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EX
   // Write the data to a temporary file.
   $temp_name = drupal_tempnam('temporary://', 'file');
   if (file_put_contents($temp_name, $data) === FALSE) {
-    drupal_set_message(t('The file could not be created.'), 'error');
+    \Drupal::messenger()->addError(t('The file could not be created.'));
     return FALSE;
   }
 
diff --git a/core/includes/form.inc b/core/includes/form.inc
index d99dca282a21..052c9ce87a9a 100644
--- a/core/includes/form.inc
+++ b/core/includes/form.inc
@@ -639,7 +639,7 @@ function template_preprocess_form_element_label(&$variables) {
  *   else {
  *     $message = t('Finished with an error.');
  *   }
- *   drupal_set_message($message);
+ *   \Drupal::messenger()->addMessage($message);
  *   // Providing data for the redirected page is done through $_SESSION.
  *   foreach ($results as $result) {
  *     $items[] = t('Loaded node %title.', array('%title' => $result));
diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index a869d90b533a..6d50c81a7ff8 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -1793,7 +1793,7 @@ function install_finished(&$install_state) {
   $success_message = t('Congratulations, you installed @drupal!', [
     '@drupal' => drupal_install_profile_distribution_name(),
   ]);
-  drupal_set_message($success_message);
+  \Drupal::messenger()->addStatus($success_message);
 }
 
 /**
diff --git a/core/includes/install.inc b/core/includes/install.inc
index 3529d51a980a..c757e30f05ad 100644
--- a/core/includes/install.inc
+++ b/core/includes/install.inc
@@ -1013,7 +1013,7 @@ function drupal_check_module($module) {
         if (isset($requirement['value']) && $requirement['value']) {
           $message = t('@requirements_message (Currently using @item version @version)', ['@requirements_message' => $requirement['description'], '@item' => $requirement['title'], '@version' => $requirement['value']]);
         }
-        drupal_set_message($message, 'error');
+        \Drupal::messenger()->addError($message);
       }
     }
     return FALSE;
diff --git a/core/rebuild.php b/core/rebuild.php
index 689540bc44e4..6f37360ca643 100644
--- a/core/rebuild.php
+++ b/core/rebuild.php
@@ -51,7 +51,7 @@
   array_map('call_user_func', array_filter($user_caches, 'is_callable'));
 
   drupal_rebuild($autoloader, $request);
-  drupal_set_message('Cache rebuild complete.');
+  \Drupal::messenger()->addStatus('Cache rebuild complete.');
 }
 $base_path = dirname(dirname($request->getBaseUrl()));
 header('Location: ' . $base_path);
-- 
GitLab