Skip to content

Commit

Permalink
Merge branch 'release/1.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Sep 7, 2021
2 parents 15748fd + 27e9d1e commit 5c377d5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# v1.8.0
## 09/07/2021

1. [](#new)
* Require **Grav 1.7.0**
* Added support for `{% throw 404 'Not Found' %}` from twig template to show the error page
1. [](#improved)
* Do not cache 404 error pages by default

# v1.7.1
## 10/08/2020

Expand Down
4 changes: 2 additions & 2 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Error
version: 1.7.1
version: 1.8.0
description: Displays the error page.
type: plugin
slug: error
Expand All @@ -13,7 +13,7 @@ keywords: error, plugin, required
bugs: https://github.com/getgrav/grav-plugin-error/issues
license: MIT
dependencies:
- { name: grav, version: '>=1.6.0' }
- { name: grav, version: '>=1.7.0' }

form:
validation: strict
Expand Down
55 changes: 41 additions & 14 deletions error.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Grav\Plugin;

use Composer\Autoload\ClassLoader;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
Expand All @@ -13,7 +14,7 @@ class ErrorPlugin extends Plugin
/**
* @return array
*/
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
'onCliInitialize' => [
Expand All @@ -27,6 +28,9 @@ public static function getSubscribedEvents()
],
'onTwigTemplatePaths' => [
['onTwigTemplatePaths', -10]
],
'onDisplayErrorPage.404'=> [
['onDisplayErrorPage404', -1]
]
];
}
Expand All @@ -36,44 +40,67 @@ public static function getSubscribedEvents()
*
* @return ClassLoader
*/
public function autoload()
public function autoload(): ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
}

/**
* @param Event $event
*/
public function onDisplayErrorPage404(Event $event): void
{
if ($this->isAdmin()) {
return;
}

$event['page'] = $this->getErrorPage();
$event->stopPropagation();
}

/**
* Display error page if no page was found for the current route.
*
* @param Event $event
*/
public function onPageNotFound(Event $event)
public function onPageNotFound(Event $event): void
{
$event->page = $this->getErrorPage();
$event->stopPropagation();
}

/**
* @return PageInterface
* @throws \Exception
*/
public function getErrorPage(): PageInterface
{
/** @var Pages $pages */
$pages = $this->grav['pages'];

// Try to load user error page.
$page = $pages->dispatch($this->config->get('plugins.error.routes.404', '/error'), true);
if ($page) {
// Set default expires for error page.
$header = $page->header();
if (!isset($header->expires)) {
$page->expires(0);
}
} else {
if (!$page) {
// If none provided use built in error page.
$page = new Page;
$page->init(new \SplFileInfo(__DIR__ . '/pages/error.md'));
$page->title($this->grav['language']->translate('PLUGIN_ERROR.ERROR') . ' ' . $page->header()->http_response_code);

}

$event->page = $page;
$event->stopPropagation();
// Login page may not have the correct Cache-Control header set, force no-store for the proxies.
$cacheControl = $page->cacheControl();
if (!$cacheControl) {
$page->cacheControl('private, no-cache, must-revalidate');
}

return $page;
}

/**
* Add page template types.
*/
public function onGetPageTemplates(Event $event)
public function onGetPageTemplates(Event $event): void
{
/** @var Types $types */
$types = $event->types;
Expand All @@ -83,7 +110,7 @@ public function onGetPageTemplates(Event $event)
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
public function onTwigTemplatePaths(): void
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
Expand Down

0 comments on commit 5c377d5

Please sign in to comment.