diff --git a/CHANGELOG.md b/CHANGELOG.md index bbcd7980..d452d74b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Fixed a bug where front-end links included element reference URL fragments. ([#197](https://github.com/craftcms/ckeditor/issues/197)) +- Fixed a bug where `resave` commands would replace nested entries with their rendered output in stored CKEditor field values. ([#211](https://github.com/craftcms/ckeditor/issues/211)) ## 4.0.3 - 2024-03-28 diff --git a/src/Field.php b/src/Field.php index 02760425..b0f92ec8 100644 --- a/src/Field.php +++ b/src/Field.php @@ -649,7 +649,7 @@ public function getSettings(): array */ public function normalizeValue(mixed $value, ?ElementInterface $element = null): mixed { - if (!$this->isCpRequest()) { + if (is_string($value) && $this->isSiteRequest()) { $value = $this->_prepNestedEntriesForDisplay($value, $element?->siteId); } @@ -1047,7 +1047,7 @@ private function _prepNestedEntriesForDisplay(string $value, ?int $elementSiteId return $value; } - $isCpRequest = $this->isCpRequest(); + $isSiteRequest = $this->isSiteRequest(); $entries = Entry::find() ->id($entryIds) ->siteId($elementSiteId) @@ -1063,9 +1063,11 @@ private function _prepNestedEntriesForDisplay(string $value, ?int $elementSiteId /** @var Entry|null $entry */ $entry = $entries[$entryId] ?? null; - if (!$entry || (!$isCpRequest && $entry->trashed)) { + if (!$entry || ($isSiteRequest && $entry->trashed)) { $entryHtml = ''; - } elseif ($isCpRequest) { + } elseif ($isSiteRequest) { + $entryHtml = $entry->render(); + } else { try { $entryHtml = $this->getCardHtml($entry); @@ -1081,8 +1083,6 @@ private function _prepNestedEntriesForDisplay(string $value, ?int $elementSiteId // this can happen e.g. when the entry type has been deleted $entryHtml = ''; } - } else { - $entryHtml = $entry->render(); } $value = str_replace($marker, $entryHtml, $value); @@ -1106,11 +1106,11 @@ private function defaultCreateButtonLabel(): string ]); } - private function isCpRequest(): bool + private function isSiteRequest(): bool { return ( - Craft::$app->getRequest()->getIsCpRequest() && - !Craft::$app->controller instanceof GraphqlController + Craft::$app->getRequest()->getIsSiteRequest() || + Craft::$app->controller instanceof GraphqlController ); }