NEW: Auto-injection into template

This commit is contained in:
Aaron Carlino 2019-06-28 15:43:21 +12:00
parent db3c7d8e39
commit 22d21d1050
3 changed files with 68 additions and 7 deletions

View File

@ -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:

View File

@ -16,7 +16,7 @@
}
],
"require": {
"silverstripe/framework": "^4.0"
"silverstripe/framework": ">= 4.2"
},
"extra": {
"installer-name": "betternavigator",

View File

@ -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 </body> 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