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: correct preview view when using subsites #1181

Open
wants to merge 1 commit into
base: 5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/BaseElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public function previewLinksProvider()
[
ElementContent::class,
'content1',
'/test-elemental',
'test-elemental',
],
// Element in DataObject WITHOUT PreviewLink or Link
[
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/Src/TestPreviewableDataObjectWithLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down