Skip to content

Instantly share code, notes, and snippets.

@unclecheese
Last active June 27, 2019 00:38
Show Gist options
  • Save unclecheese/c50dd373d0cb0895772467960aa4b6db to your computer and use it in GitHub Desktop.
Save unclecheese/c50dd373d0cb0895772467960aa4b6db to your computer and use it in GitHub Desktop.
<?php
namespace SilverStripe\Bambusa\Extensions;
use JonoM\BetterNavigator\Extension\BetterNavigatorExtension;
use SilverStripe\ORM\Extension;
use SilverStripe\ORM\FieldType\DBHTMLText;
class NavigatorInjection extends Extension
{
/**
* @var string
*/
private $navigatorHTML;
/**
* Prerender HTML to ensure that <% require %> gets loaded
*/
public function beforeCallActionHandler(): void
{
$navigator = $this->owner->BetterNavigator();
if ($navigator) {
$this->navigatorHTML = $navigator->getValue();
}
}
/**
* @param $request
* @param $action
* @param $result
* @return DBHTMLText
*/
public function afterCallActionHandler($request, $action, $result): DBHTMLText
{
$hasExtension = $this->owner->hasExtension(BetterNavigatorExtension::class);
if (!$hasExtension || !$this->navigatorHTML) {
return $result;
}
/* @var DBHTMLText $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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment