From 5614f82871b6daf30ebcd86cd5365c7bc6ad9f40 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Thu, 30 May 2019 12:17:14 +0100 Subject: [PATCH] Issue #2349725 by AdamPS, alexpott, hexblot, jonathanshaw, Berdir, larowlan, andypost: Core Mail class prints non-overridable errors on the page --- core/lib/Drupal/Core/Mail/MailManager.php | 9 +++++++-- .../Drupal/Core/Mail/MailManagerInterface.php | 4 +++- .../tests/src/Functional/Mail/MailTest.php | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/core/lib/Drupal/Core/Mail/MailManager.php b/core/lib/Drupal/Core/Mail/MailManager.php index 5cf3c9ea05b..4fd4f9ba7e6 100644 --- a/core/lib/Drupal/Core/Mail/MailManager.php +++ b/core/lib/Drupal/Core/Mail/MailManager.php @@ -202,7 +202,9 @@ class MailManager extends DefaultPluginManager implements MailManagerInterface { * @param string $langcode * Language code to use to compose the email. * @param array $params - * (optional) Parameters to build the email. + * (optional) Parameters to build the email. Use the key '_error_message' + * to provide translatable markup to display as a message if an error + * occurs, or set this to false to disable error display. * @param string|null $reply * Optional email address to be used to answer. * @param bool $send @@ -311,7 +313,10 @@ class MailManager extends DefaultPluginManager implements MailManagerInterface { '%to' => $message['to'], '%reply' => $message['reply-to'] ? $message['reply-to'] : $this->t('not set'), ]); - $this->messenger()->addError($this->t('Unable to send email. Contact the site administrator if the problem persists.')); + $error_message = $params['_error_message'] ?? $this->t('Unable to send email. Contact the site administrator if the problem persists.'); + if ($error_message) { + $this->messenger()->addError($error_message); + } } } } diff --git a/core/lib/Drupal/Core/Mail/MailManagerInterface.php b/core/lib/Drupal/Core/Mail/MailManagerInterface.php index eadba56f001..6ff083d8cbb 100644 --- a/core/lib/Drupal/Core/Mail/MailManagerInterface.php +++ b/core/lib/Drupal/Core/Mail/MailManagerInterface.php @@ -102,7 +102,9 @@ interface MailManagerInterface extends PluginManagerInterface { * @param string $langcode * Language code to use to compose the email. * @param array $params - * (optional) Parameters to build the email. + * (optional) Parameters to build the email. Use the key '_error_message' + * to provide translatable markup to display as a message if an error + * occurs, or set this to false to disable error display. * @param string|null $reply * Optional email address to be used to answer. * @param bool $send diff --git a/core/modules/system/tests/src/Functional/Mail/MailTest.php b/core/modules/system/tests/src/Functional/Mail/MailTest.php index ef59d0128d1..a821c686116 100644 --- a/core/modules/system/tests/src/Functional/Mail/MailTest.php +++ b/core/modules/system/tests/src/Functional/Mail/MailTest.php @@ -6,6 +6,7 @@ use Drupal\Component\Utility\Random; use Drupal\Component\Utility\Unicode; use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Mail\Plugin\Mail\TestMailCollector; +use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Render\Markup; use Drupal\Core\Url; use Drupal\file\Entity\File; @@ -51,6 +52,24 @@ class MailTest extends BrowserTestBase { $this->assertTrue($mail_backend instanceof TestMailCollector, 'Additional mail interfaces can be added.'); } + /** + * Assert that the pluggable mail system is functional. + */ + public function testErrorMessageDisplay() { + // Switch mail backends. + $this->config('system.mail')->set('interface.default', 'test_php_mail_failure')->save(); + + // Test with errors displayed to users. + \Drupal::service('plugin.manager.mail')->mail('default', 'default', 'test@example.com', 'en'); + $messages = \Drupal::messenger()->messagesByType(MessengerInterface::TYPE_ERROR); + $this->assertEquals('Unable to send email. Contact the site administrator if the problem persists.', $messages[0]); + \Drupal::messenger()->deleteAll(); + + // Test without errors displayed to users. + \Drupal::service('plugin.manager.mail')->mail('default', 'default', 'test@example.com', 'en', ['_error_message' => '']); + $this->assertEmpty(\Drupal::messenger()->messagesByType(MessengerInterface::TYPE_ERROR)); + } + /** * Test that message sending may be canceled. *