Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the allow_twig option to work in the preview #2486

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions src/Controller/Backend/ContentEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Entity/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
6 changes: 3 additions & 3 deletions src/Event/Listener/FieldFillListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand Down