diff --git a/src/Extension/BetterNavigatorExtension.php b/src/Extension/BetterNavigatorExtension.php index 1297618..2e95f42 100644 --- a/src/Extension/BetterNavigatorExtension.php +++ b/src/Extension/BetterNavigatorExtension.php @@ -6,9 +6,10 @@ use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; +use SilverStripe\Control\HTTPResponse; use SilverStripe\Core\Config\Config; +use SilverStripe\Core\Manifest\ModuleLoader; use SilverStripe\ORM\DataExtension; -use SilverStripe\ORM\FieldType\DBField; use SilverStripe\Security\LogoutForm; use SilverStripe\Security\Member; use SilverStripe\Security\Permission; @@ -26,35 +27,23 @@ class BetterNavigatorExtension extends DataExtension */ private $shouldDisplay = null; - /** - * Load requirements in before final render. When the next extension point is called, it's too late. - * @return void - */ - public function beforeCallActionHandler() - { - if ($this->shouldDisplay()) { - Requirements::javascript('jonom/silverstripe-betternavigator: javascript/betternavigator.js'); - Requirements::css('jonom/silverstripe-betternavigator: css/betternavigator.css'); - } - } - /** * @param $request * @param $action - * @param DBHTMLText $result - * @return DBHTMLText + * @param DBHTMLText|HTTPResponse $result + * @return DBHTMLText|HTTPResponse */ public function afterCallActionHandler($request, $action, $result) { - if (!$this->shouldDisplay()) { - return $result; - } + // Check that we're dealing with HTML + $isHtmlResponse = $result instanceof DBHTMLText || + $result instanceof HTTPResponse && strpos($result->getHeader('content-type'), 'text/html') !== false; - if (!($result instanceof DBHTMLText)) { + if (!$isHtmlResponse || !$this->shouldDisplay()) { return $result; } - $html = $result->getValue(); + $html = $result instanceof DBHTMLText ? $result->getValue() : $result->getBody(); $navigatorHTML = $this->generateNavigator()->getValue(); // Inject the NavigatorHTML before the closing tag @@ -63,7 +52,11 @@ public function afterCallActionHandler($request, $action, $result) $navigatorHTML . '\\1', $html ); - $result->setValue($html); + if ($result instanceof DBHTMLText) { + $result->setValue($html); + } else { + $result->setBody($html); + } return $result; } @@ -120,6 +113,7 @@ private function generateNavigator() $backURL = '?BackURL=' . urlencode($this->owner->Link()); $logoutForm = LogoutForm::create($this->owner)->setName('BetterNavigatorLogoutForm'); $logoutForm->Fields()->fieldByName('BackURL')->setValue($this->owner->Link()); + $bnModule = ModuleLoader::getModule('jonom/silverstripe-betternavigator'); $bNData = array_merge($nav, [ 'Member' => $member, 'Stage' => Versioned::get_stage(), @@ -129,7 +123,9 @@ private function generateNavigator() 'LogoutForm' => $logoutForm, 'EditLink' => $editLink, 'Mode' => Director::get_environment_type(), - 'IsDeveloper' => $isDeveloper + 'IsDeveloper' => $isDeveloper, + 'ScriptUrl' => $bnModule->getResource('javascript/betternavigator.js')->getURL(), + 'CssUrl' => $bnModule->getResource('css/betternavigator.css')->getURL(), ]); // Merge with page data, send to template and render diff --git a/templates/BetterNavigator/BetterNavigator.ss b/templates/BetterNavigator/BetterNavigator.ss index f363547..24a7dfd 100644 --- a/templates/BetterNavigator/BetterNavigator.ss +++ b/templates/BetterNavigator/BetterNavigator.ss @@ -86,3 +86,6 @@ + + +