From 8e435ed9f934e77ad6df7f13e1ba3b1ddb03dfd2 Mon Sep 17 00:00:00 2001 From: Franco Springveldt Date: Fri, 13 Oct 2017 14:01:08 +1300 Subject: [PATCH] FIX versioned class not found and preview session bugfix --- src/Controllers/ShareDraftController.php | 20 +++++++++++++------ .../ShareDraftContentSiteTreeExtension.php | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Controllers/ShareDraftController.php b/src/Controllers/ShareDraftController.php index fad3187..2f8fe24 100644 --- a/src/Controllers/ShareDraftController.php +++ b/src/Controllers/ShareDraftController.php @@ -2,6 +2,7 @@ namespace SilverStripe\ShareDraftContent\Controllers; +use BadMethodCallException; use Exception; use PageController; use SilverStripe\CMS\Controllers\ModelAsController; @@ -9,8 +10,9 @@ use SilverStripe\Control\Controller; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\Session; -use SilverStripe\ORM\Versioning\Versioned; +use SilverStripe\Core\Injector\Injector; use SilverStripe\ShareDraftContent\Models\ShareToken; +use SilverStripe\Versioned\Versioned; use SilverStripe\View\ArrayData; use SilverStripe\View\Requirements; @@ -48,7 +50,12 @@ public function preview(HTTPRequest $request) { $key = $request->param('Key'); $token = $request->param('Token'); - + try { + $session = $this->getRequest()->getSession(); + } catch (BadMethodCallException $e) { + // Create a new session + $session = $this->getRequest()->setSession(Injector::inst()->create(Session::class)); + } /** * @var ShareToken $shareToken */ @@ -72,16 +79,16 @@ public function preview(HTTPRequest $request) Requirements::css('silverstripe/sharedraftcontent:css/top-bar.css'); // Temporarily un-secure the draft site and switch to draft - $oldSecured = Session::get('unsecuredDraftSite'); + $oldSecured = $session->get('unsecuredDraftSite'); $oldMode = Versioned::get_reading_mode(); - $restore = function () use ($oldSecured, $oldMode) { - Session::set('unsecuredDraftSite', $oldSecured); + $restore = function () use ($oldSecured, $oldMode, $session) { + $session->set('unsecuredDraftSite', $oldSecured); Versioned::set_reading_mode($oldMode); }; // Process page inside an unsecured draft container try { - Session::set('unsecuredDraftSite', true); + $session->set('unsecuredDraftSite', true); Versioned::set_stage('Stage'); // Hack to get around ContentController::init() redirecting on home page @@ -90,6 +97,7 @@ public function preview(HTTPRequest $request) // Create mock request; Simplify request to single top level request $pageRequest = new HTTPRequest('GET', $page->URLSegment); $pageRequest->match('$URLSegment//$Action/$ID/$OtherID', true); + $pageRequest->setSession($session); $rendered = $controller->handleRequest($pageRequest, $this->model); // Render draft heading diff --git a/src/Extensions/ShareDraftContentSiteTreeExtension.php b/src/Extensions/ShareDraftContentSiteTreeExtension.php index 958821e..efb8290 100644 --- a/src/Extensions/ShareDraftContentSiteTreeExtension.php +++ b/src/Extensions/ShareDraftContentSiteTreeExtension.php @@ -83,7 +83,7 @@ protected function getNewShareToken() ))->first(); } - $config = Config::inst()->forClass(__CLASS__); + $config = Config::forClass(__CLASS__); $validForDays = $config->valid_for_days;