Skip to content

Commit

Permalink
FIX Preview email link now handles cases where it's loaded in the bro…
Browse files Browse the repository at this point in the history
…wser, requested via AJAX and used in a trait or a page context (silverstripe#887)

FIX Preview email link now handles cases where it's loaded in the browser, requested via AJAX and used in a trait or a page context
  • Loading branch information
ScopeyNZ authored May 27, 2019
2 parents f4cd7a3 + 483fbc8 commit d7c76ec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions code/Model/Recipient/EmailRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,21 @@ public function getCMSFields()

// Only show the preview link if the recipient has been saved.
if (!empty($this->EmailTemplate)) {
$request = Controller::curr()->getRequest();

$pageEditController = singleton(CMSPageEditController::class);
$pageEditController
->getRequest()
->setSession(Controller::curr()->getRequest()->getSession());
$pageEditController->getRequest()->setSession($request->getSession());

$currentUrl = $request->getURL();
// If used in a regular page context, will have "/edit" on the end, if used in a trait context
// it won't. Strip that off in case.
$currentUrl = Controller::curr()->getRequest()->getURL();
if (substr($currentUrl, -5) === '/edit') {
$currentUrl = substr($currentUrl, 0, strlen($currentUrl) - 5);
// it won't. Strip that off in case. It may also have "ItemEditForm" on the end instead if this is
// an AJAX request, e.g. saving a GridFieldDetailForm
$remove = ['/edit', '/ItemEditForm'];
foreach ($remove as $badSuffix) {
$badSuffixLength = strlen($badSuffix);
if (substr($currentUrl, -$badSuffixLength) === $badSuffix) {
$currentUrl = substr($currentUrl, 0, -$badSuffixLength);
}
}
$previewUrl = Controller::join_links($currentUrl, 'preview');

Expand Down
2 changes: 1 addition & 1 deletion tests/Model/Recipient/EmailRecipientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testShortcodesAreRenderedInEmailPreviewContent()
}

/**
* @expectedException SilverStripe\ORM\ValidationException
* @expectedException \SilverStripe\ORM\ValidationException
* @expectedExceptionMessage "Send email to" address or field is required
*/
public function testEmptyRecipientFailsValidation()
Expand Down

0 comments on commit d7c76ec

Please sign in to comment.