Commit 3137a14d authored by alexpott's avatar alexpott

Issue #2554755 by andypost, lauriii, joelpittet: BasicStringFormatter's...

Issue #2554755 by andypost, lauriii, joelpittet: BasicStringFormatter's nl2br() forces escaping *and* filtering — improve this
parent 16483763
......@@ -7,7 +7,6 @@
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
......@@ -32,12 +31,16 @@ class BasicStringFormatter extends FormatterBase {
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
$elements = [];
foreach ($items as $delta => $item) {
// The text value has no text format assigned to it, so the user input
// should equal the output, including newlines.
$elements[$delta] = array('#markup' => nl2br(Html::escape($item->value)));
$elements[$delta] = [
'#type' => 'inline_template',
'#template' => '{{ value|nl2br }}',
'#context' => ['value' => $item->value],
];
}
return $elements;
......
......@@ -7,9 +7,7 @@
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FormatterBase;
......@@ -137,7 +135,7 @@ public function viewElements(FieldItemListInterface $items) {
];
}
else {
$elements[$delta] = is_array($view_value) ? $view_value : ['#markup' => $view_value];
$elements[$delta] = $view_value;
}
}
return $elements;
......@@ -156,7 +154,9 @@ protected function viewValue(FieldItemInterface $item) {
// The text value has no text format assigned to it, so the user input
// should equal the output, including newlines.
return [
'#markup' => nl2br(Html::escape($item->value))
'#type' => 'inline_template',
'#template' => '{{ value|nl2br }}',
'#context' => ['value' => $item->value],
];
}
......
......@@ -46,13 +46,19 @@ public function testUuidStringFormatter() {
$uuid_field = $entity->get('uuid');
// Verify default render.
$render_array = $uuid_field->view([]);
$this->assertIdentical($render_array[0]['#markup'], $entity->uuid(), 'The rendered UUID matches the entity UUID.');
$this->assertIdentical($render_array[0]['#context']['value'], $entity->uuid(), 'The rendered UUID matches the entity UUID.');
$this->assertTrue(strpos($this->render($render_array), $entity->uuid()), 'The rendered UUID found.');
// Verify customized render.
$render_array = $uuid_field->view(['settings' => ['link_to_entity' => TRUE]]);
$this->assertIdentical($render_array[0]['#type'], 'link');
$this->assertIdentical($render_array[0]['#title']['#markup'], $entity->uuid());
$this->assertIdentical($render_array[0]['#title']['#context']['value'], $entity->uuid());
$this->assertIdentical($render_array[0]['#url']->toString(), $entity->url());
$rendered = $this->render($render_array);
$this->assertTrue(strpos($rendered, $entity->uuid()), 'The rendered UUID found.');
$this->assertTrue(strpos($rendered, $entity->url()), 'The rendered entity URL found.');
}
}
......@@ -28,7 +28,8 @@ public function getItems(ResultRow $values) {
foreach ($items as &$item) {
// Replace spaces with hyphens.
$name = $item['raw']->get('value')->getValue();
$item['rendered']['#markup'] = str_replace(' ', '-', $name);
// @todo Add link support https://www.drupal.org/node/2567745
$item['rendered']['#context']['value'] = str_replace(' ', '-', $name);
}
}
return $items;
......
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