Commit 248506e1 authored by xjm's avatar xjm

Issue #2550981 by alexpott, stefan.r, davidhernandez, joelpittet, Wim Leers,...

Issue #2550981 by alexpott, stefan.r, davidhernandez, joelpittet, Wim Leers, xjm, josephdpurcell, akalata, dawehner: Remove SafeMarkup::set in FormErrorHandler::displayErrorMessages()
parent f82e0a3a
......@@ -338,7 +338,7 @@ services:
arguments: ['@request_stack', '@url_generator']
form_error_handler:
class: Drupal\Core\Form\FormErrorHandler
arguments: ['@string_translation', '@link_generator']
arguments: ['@string_translation', '@link_generator', '@renderer']
form_cache:
class: Drupal\Core\Form\FormCache
arguments: ['@app.root', '@keyvalue.expirable', '@module_handler', '@current_user', '@csrf_token', '@logger.channel.form', '@request_stack', '@page_cache_request_policy']
......
......@@ -7,9 +7,9 @@
namespace Drupal\Core\Form;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\LinkGeneratorTrait;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
......@@ -23,6 +23,13 @@ class FormErrorHandler implements FormErrorHandlerInterface {
use StringTranslationTrait;
use LinkGeneratorTrait;
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Constructs a new FormErrorHandler.
*
......@@ -30,10 +37,13 @@ class FormErrorHandler implements FormErrorHandlerInterface {
* The string translation service.
* @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
* The link generation service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/
public function __construct(TranslationInterface $string_translation, LinkGeneratorInterface $link_generator) {
public function __construct(TranslationInterface $string_translation, LinkGeneratorInterface $link_generator, RendererInterface $renderer) {
$this->stringTranslation = $string_translation;
$this->linkGenerator = $link_generator;
$this->renderer = $renderer;
}
/**
......@@ -82,9 +92,7 @@ protected function displayErrorMessages(array $form, FormStateInterface $form_st
unset($errors[$name]);
}
elseif ($is_visible_element && $has_title && $has_id) {
// We need to pass this through SafeMarkup::escape() so
// drupal_set_message() does not encode the links.
$error_links[] = SafeMarkup::escape($this->l($title, Url::fromRoute('<none>', [], ['fragment' => $form_element['#id'], 'external' => TRUE])));
$error_links[] = $this->l($title, Url::fromRoute('<none>', [], ['fragment' => $form_element['#id'], 'external' => TRUE]));
unset($errors[$name]);
}
}
......@@ -95,9 +103,17 @@ protected function displayErrorMessages(array $form, FormStateInterface $form_st
}
if (!empty($error_links)) {
$message = $this->formatPlural(count($error_links), '1 error has been found: !errors', '@count errors have been found: !errors', [
'!errors' => SafeMarkup::set(implode(', ', $error_links)),
]);
$render_array = [
[
'#markup' => $this->formatPlural(count($error_links), '1 error has been found: ', '@count errors have been found: '),
],
[
'#theme' => 'item_list',
'#items' => $error_links,
'#context' => ['list_style' => 'comma-list'],
],
];
$message = $this->renderer->renderPlain($render_array);
$this->drupalSetMessage($message, 'error');
}
}
......
......@@ -589,13 +589,13 @@ function testDatelistWidget() {
protected function datelistDataProvider() {
return [
// Year only selected, validation error on Month, Day, Hour, Minute.
[['year' => 2012, 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''], '4 errors have been found: Month, Day, Hour, Minute'],
[['year' => 2012, 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''], '4 errors have been found: MonthDayHourMinute'],
// Year and Month selected, validation error on Day, Hour, Minute.
[['year' => 2012, 'month' => '12', 'day' => '', 'hour' => '', 'minute' => ''], '3 errors have been found: Day, Hour, Minute'],
[['year' => 2012, 'month' => '12', 'day' => '', 'hour' => '', 'minute' => ''], '3 errors have been found: DayHourMinute'],
// Year, Month and Day selected, validation error on Hour, Minute.
[['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => ''], '2 errors have been found: Hour, Minute'],
[['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => ''], '2 errors have been found: HourMinute'],
// Year, Month, Day and Hour selected, validation error on Minute only.
[['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => ''], '1 error has been found: Minute'],
[['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => ''], '1 error has been found: Minute'],
];
}
......
......@@ -275,7 +275,7 @@ public function testFieldAdminHandler() {
$this->drupalPostForm('node/add/' . $this->type, $edit, t('Save'));
// Assert that entity reference autocomplete field is validated.
$this->assertText(t('1 error has been found: Test Entity Reference Field'), 'Node save failed when required entity reference field was not correctly filled.');
$this->assertText(t('1 error has been found: Test Entity Reference Field'), 'Node save failed when required entity reference field was not correctly filled.');
$this->assertText(t('There are no entities matching "@entity"', ['@entity' => 'Test']));
$edit = array(
......@@ -286,7 +286,7 @@ public function testFieldAdminHandler() {
// Assert the results multiple times to avoid sorting problem of nodes with
// the same title.
$this->assertText(t('1 error has been found: Test Entity Reference Field'));
$this->assertText(t('1 error has been found: Test Entity Reference Field'));
$this->assertText(t('Multiple entities match this reference;'));
$this->assertText(t("@node1", ['@node1' => $node1->getTitle() . ' (' . $node1->id() . ')']));
$this->assertText(t("@node2", ['@node2' => $node2->getTitle() . ' (' . $node2->id() . ')']));
......
......@@ -35,7 +35,7 @@ function testRequired() {
$edit = array();
$edit['title[0][value]'] = $this->randomMachineName();
$this->drupalPostForm('node/add/' . $type_name, $edit, t('Save and publish'));
$this->assertText('1 error has been found: ' . $field->label(), 'Node save failed when required file field was empty.');
$this->assertText('1 error has been found: ' . $field->label(), 'Node save failed when required file field was empty.');
$this->assertIdentical(1, count($this->xpath('//div[contains(concat(" ", normalize-space(@class), " "), :class)]//a', [':class' => ' messages--error '])), 'There is one link in the error message.');
// Create a new node with the uploaded file.
......@@ -57,7 +57,7 @@ function testRequired() {
$edit = array();
$edit['title[0][value]'] = $this->randomMachineName();
$this->drupalPostForm('node/add/' . $type_name, $edit, t('Save and publish'));
$this->assertText('1 error has been found: ' . $field->label(), 'Node save failed when required multiple value file field was empty.');
$this->assertText('1 error has been found: ' . $field->label(), 'Node save failed when required multiple value file field was empty.');
$this->assertIdentical(1, count($this->xpath('//div[contains(concat(" ", normalize-space(@class), " "), :class)]//a', [':class' => ' messages--error '])), 'There is one link in the error message.');
// Create a new node with the uploaded file into the multivalue field.
......
......@@ -132,7 +132,8 @@ function testShortcutSetSwitchCreate() {
function testShortcutSetSwitchNoSetName() {
$edit = array('set' => 'new');
$this->drupalPostForm('user/' . $this->adminUser->id() . '/shortcuts', $edit, t('Change set'));
$this->assertRaw('1 error has been found: <a href="#edit-label">Label</a>');
$this->assertRaw('1 error has been found:');
$this->assertRaw('<a href="#edit-label">Label</a>');
$current_set = shortcut_current_displayed_set($this->adminUser);
$this->assertEqual($current_set->id(), $this->set->id(), 'Attempting to switch to a new shortcut set without providing a set name does not succeed.');
$this->assertFieldByXPath("//input[@name='label' and contains(concat(' ', normalize-space(@class), ' '), ' error ')]", NULL, 'The new set label field has the error class');
......
......@@ -292,7 +292,10 @@ protected function assertErrorMessages($messages) {
$error_links[] = \Drupal::l($message['title'], Url::fromRoute('<none>', [], ['fragment' => 'edit-' . str_replace('_', '-', $message['key']), 'external' => TRUE]));
}
$top_message = \Drupal::translation()->formatPlural(count($error_links), '1 error has been found:', '@count errors have been found:');
$this->assertRaw($top_message . ' ' . implode(', ', $error_links));
$this->assertRaw($top_message);
foreach ($error_links as $error_link) {
$this->assertRaw($error_link);
}
$this->assertNoText('An illegal choice has been detected. Please contact the site administrator.');
}
......
......@@ -48,7 +48,8 @@ function testUserLoginBlock() {
$edit['name'] = $this->randomMachineName();
$edit['pass'] = $this->randomMachineName();
$this->drupalPostForm('node', $edit, t('Log in'));
$this->assertRaw('1 error has been found: <a href="#edit-name">Username</a>');
$this->assertRaw('1 error has been found:');
$this->assertRaw('<a href="#edit-name">Username</a>');
$this->assertText(t('Sorry, unrecognized username or password.'));
// Create a user with some permission that anonymous users lack.
......
......@@ -25,8 +25,9 @@ public function testDisplayErrorMessages() {
$link_generator->expects($this->any())
->method('generate')
->willReturnArgument(0);
$renderer = $this->getMock('\Drupal\Core\Render\RendererInterface');
$form_error_handler = $this->getMockBuilder('Drupal\Core\Form\FormErrorHandler')
->setConstructorArgs([$this->getStringTranslationStub(), $link_generator])
->setConstructorArgs([$this->getStringTranslationStub(), $link_generator, $renderer])
->setMethods(['drupalSetMessage'])
->getMock();
......@@ -41,7 +42,13 @@ public function testDisplayErrorMessages() {
->with('this missing element is invalid', 'error');
$form_error_handler->expects($this->at(3))
->method('drupalSetMessage')
->with('3 errors have been found: Test 1, Test 2 &amp; a half, Test 3', 'error');
->with('3 errors have been found: <ul-comma-list-mock><li-mock>Test 1</li-mock><li-mock>Test 2 &amp; a half</li-mock><li-mock>Test 3</li-mock></ul-comma-list-mock>', 'error');
$renderer->expects($this->any())
->method('renderPlain')
->will($this->returnCallback(function ($render_array) {
return $render_array[0]['#markup'] . '<ul-comma-list-mock><li-mock>' . implode(array_map('htmlspecialchars', $render_array[1]['#items']), '</li-mock><li-mock>') . '</li-mock></ul-comma-list-mock>';
}));
$form = [
'#parents' => [],
......@@ -103,7 +110,7 @@ public function testDisplayErrorMessages() {
*/
public function testSetElementErrorsFromFormState() {
$form_error_handler = $this->getMockBuilder('Drupal\Core\Form\FormErrorHandler')
->setConstructorArgs([$this->getStringTranslationStub(), $this->getMock('Drupal\Core\Utility\LinkGeneratorInterface')])
->setConstructorArgs([$this->getStringTranslationStub(), $this->getMock('Drupal\Core\Utility\LinkGeneratorInterface'), $this->getMock('\Drupal\Core\Render\RendererInterface')])
->setMethods(['drupalSetMessage'])
->getMock();
......
......@@ -38,6 +38,7 @@ ul.inline li {
ul.inline li {
display: inline;
}
[dir="rtl"] ul.item-list__comma-list {
.item-list .item-list__comma-list,
[dir="rtl"] .item-list .item-list__comma-list {
margin: 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment