From 22d21d1050f0ebe86e47744d0dc3f79684aa3465 Mon Sep 17 00:00:00 2001 From: Aaron Carlino Date: Fri, 28 Jun 2019 15:43:21 +1200 Subject: [PATCH] NEW: Auto-injection into template --- README.md | 6 +- composer.json | 2 +- src/Extension/BetterNavigatorExtension.php | 67 ++++++++++++++++++++-- 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 409ce7d..1ecc785 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ This module is intended to replicate and expand upon the functionality provided ## Requirements -SilverStripe 4.0 (3.1+ through previous releases) +SilverStripe 4.2 (3.1+ through previous releases) ## Installation @@ -29,7 +29,9 @@ Download, place the folder in your project root, rename it to 'betternavigator' ## How to use -Just place **$BetterNavigator** somewhere in your template(s). If your website uses caching, make sure BetterNavigator's output is excluded. +The navigator is auto-injected into your template, and no code changes are needed. + +If your website uses caching, make sure BetterNavigator's output is excluded. **Access developer tools on a live website** You can mark certain CMS users as developers in your site's config, so they can access developer tools when logged in. Example YAML: diff --git a/composer.json b/composer.json index 86433ee..a08fca3 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "silverstripe/framework": "^4.0" + "silverstripe/framework": ">= 4.2" }, "extra": { "installer-name": "betternavigator", diff --git a/src/Extension/BetterNavigatorExtension.php b/src/Extension/BetterNavigatorExtension.php index 46000fe..4d6c9b0 100644 --- a/src/Extension/BetterNavigatorExtension.php +++ b/src/Extension/BetterNavigatorExtension.php @@ -8,22 +8,40 @@ use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Config; use SilverStripe\ORM\DataExtension; +use SilverStripe\ORM\FieldType\DBField; use SilverStripe\Security\LogoutForm; use SilverStripe\Security\Member; use SilverStripe\Security\Permission; use SilverStripe\Security\Security; use SilverStripe\Versioned\Versioned; use SilverStripe\View\ArrayData; +use SilverStripe\ORM\FieldType\DBHTMLText; -class BetterNavigatorExtension extends DataExtension { +class BetterNavigatorExtension extends DataExtension +{ + + /** + * @var string + */ + private $navigatorHTML; + + /** + * Noop, pseudo backward compatability only + * @return DBHTMLText + */ + public function BetterNavigator() + { + return DBField::create_field('HTMLText', ''); + } /** * Provides a front-end utility menu with administrative functions and developer tools * Relies on SilverStripeNavigator * - * @return string + * @return DBHTMLText|false */ - public function BetterNavigator() { + protected function generateNavigator() + { // Make sure this is a page if (!$this->isAPage()) return false; @@ -83,7 +101,48 @@ class BetterNavigatorExtension extends DataExtension { return false; } - protected function isAPage() { + /** + * Prerender HTML to ensure that <% require %> gets loaded + * @return void + */ + public function beforeCallActionHandler(): void + { + $navigator = $this->generateNavigator(); + if ($navigator) { + $this->navigatorHTML = $navigator->getValue(); + } + } + + /** + * @param $request + * @param $action + * @param DBHTMLText $result + * @return DBHTMLText + */ + public function afterCallActionHandler($request, $action, $result): DBHTMLText + { + if (!$this->navigatorHTML) { + return $result; + } + + $html = $result->getValue(); + + // Inject the NavigatorHTML before the closing tag + $html = preg_replace( + '/(<\/body[^>]*>)/i', + $this->navigatorHTML . '\\1', + $html + ); + $result->setValue($html); + + return $result; + } + + /** + * @return boolean + */ + protected function isAPage() + { return $this->owner && $this->owner->dataRecord && $this->owner->dataRecord instanceof SiteTree