diff --git a/src/Controller/Backend/ContentEditController.php b/src/Controller/Backend/ContentEditController.php index a0ff8f7e7..e735152ae 100644 --- a/src/Controller/Backend/ContentEditController.php +++ b/src/Controller/Backend/ContentEditController.php @@ -171,19 +171,11 @@ public function preview(?Content $content = null): Response $this->validateCsrf('editrecord'); $content = $this->contentFromPost($content); - $recordSlug = $content->getDefinition()->get('singular_slug'); $event = new ContentEvent($content); $this->dispatcher->dispatch($event, ContentEvent::ON_PREVIEW); - $context = [ - 'record' => $content, - $recordSlug => $content, - ]; - - $templates = $this->templateChooser->forRecord($content); - - return $this->render($templates, $context); + return $this->renderSingle($content, false); } /** diff --git a/src/Entity/Field.php b/src/Entity/Field.php index 7496efc64..dce4bbccd 100644 --- a/src/Entity/Field.php +++ b/src/Entity/Field.php @@ -9,6 +9,7 @@ use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter; use Bolt\Common\Arr; use Bolt\Configuration\Content\FieldType; +use Bolt\Event\Listener\FieldFillListener; use Doctrine\ORM\Mapping as ORM; use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface; use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait; @@ -235,6 +236,10 @@ public function getTwigValue() $value = $this->getContent()->sanitise($value); } + // Trim the zero spaces even before saving in FieldFillListener. + // Otherwise, the preview contains zero width whitespace. + $value = is_string($value) ? FieldFillListener::trimZeroWidthWhitespace($value) : $value; + if ($this->shouldBeRenderedAsTwig($value)) { $twig = $this->getContent()->getTwig(); diff --git a/src/Event/Listener/FieldFillListener.php b/src/Event/Listener/FieldFillListener.php index ef518327f..a9228c6de 100644 --- a/src/Event/Listener/FieldFillListener.php +++ b/src/Event/Listener/FieldFillListener.php @@ -58,11 +58,11 @@ private function clean($value): array foreach ($value as $key => $v) { if ($v instanceof Markup) { - $v = $this->trimZeroWidthWhitespace((string) $v); + $v = self::trimZeroWidthWhitespace((string) $v); // todo: Figure out how to preserve original encoding $v = new Markup($this->sanitiser->clean($v), 'UTF-8'); } elseif (is_string($v)) { - $v = $this->trimZeroWidthWhitespace($v); + $v = self::trimZeroWidthWhitespace($v); $v = $this->sanitiser->clean($v); } @@ -75,7 +75,7 @@ private function clean($value): array /** * Remove the 'zero width space' from `{{` and `}}`, added in the editor. */ - public function trimZeroWidthWhitespace(string $string): string + public static function trimZeroWidthWhitespace(string $string): string { return preg_replace('/([{}])[\x{200B}-\x{200D}\x{FEFF}]([{}])/u', '$1$2', $string); }