diff --git a/src/Models/BaseElement.php b/src/Models/BaseElement.php index e1078558..9e67b4a5 100644 --- a/src/Models/BaseElement.php +++ b/src/Models/BaseElement.php @@ -553,7 +553,7 @@ public function getContentForCmsSearch(): string } // Allow projects to update contents of third-party elements. $this->extend('updateContentForCmsSearch', $contents); - + // Use |#| to delimit different fields rather than space so that you don't // accidentally join results of two columns that are next to each other in a table $content = implode('|#|', array_filter($contents)); @@ -841,12 +841,14 @@ public function PreviewLink($action = null) { $link = null; if ($page = $this->getPage()) { - if (ClassInfo::hasMethod($page, 'Link')) { - $link = $page->Link($action); - } - if (!$link && ($page instanceof CMSPreviewable)) { + if ($page instanceof CMSPreviewable) { $link = $page->PreviewLink($action); } + + if (!$link && ClassInfo::hasMethod($page, 'Link')) { + $link = $page->Link($action); + } + if ($link) { // The ElementalPreview getvar is used in ElementalPageExtension // The anchor must be at the end of the URL to function correctly diff --git a/tests/BaseElementTest.php b/tests/BaseElementTest.php index ad6ce471..6fad11f8 100644 --- a/tests/BaseElementTest.php +++ b/tests/BaseElementTest.php @@ -445,7 +445,7 @@ public function previewLinksProvider() [ ElementContent::class, 'content1', - '/test-elemental', + 'test-elemental', ], // Element in DataObject WITHOUT PreviewLink or Link [ @@ -492,8 +492,9 @@ public function testPreviewLink(string $class, string $elementIdentifier, ?strin // Note that the preview link is suffixed with forward slash when subsites is installed // because of BaseElementSubsites::updatePreviewLink() use of HTTP::setGetVar() // which turns relative urls to absolute urls - $regex = '/^\/?' . preg_quote($link, '/') . '[?&]' . preg_quote('ElementalPreview=', '/') + $regex = '/' . preg_quote($link, '/') . '[?&]' . preg_quote('ElementalPreview=', '/') .'\d*#' . $element->getAnchor() . '$/'; + $this->assertMatchesRegularExpression($regex, $previewLink); // Doesn't try to blindly append query string and anchor - but instead merges intelligently with // whatever's already there diff --git a/tests/Src/TestPreviewableDataObjectWithLink.php b/tests/Src/TestPreviewableDataObjectWithLink.php index 962afe1d..c4a35298 100644 --- a/tests/Src/TestPreviewableDataObjectWithLink.php +++ b/tests/Src/TestPreviewableDataObjectWithLink.php @@ -16,6 +16,11 @@ class TestPreviewableDataObjectWithLink extends TestPreviewableDataObject implem 'LinkData' => 'base-link', ]; + public function PreviewLink($action = null) + { + return null; + } + public function Link($action = null) { return $this->LinkData;