mirror of
https://github.com/getgrav/grav.git
synced 2026-07-05 21:48:30 +02:00
Refactored onPageNotFound event to fire after onPageInitialized
This commit is contained in:
5
system/pages/notfound.md
Normal file
5
system/pages/notfound.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
title: Not Found
|
||||
routable: false
|
||||
notfound: true
|
||||
---
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
namespace Grav\Common\Processors;
|
||||
|
||||
use Grav\Common\Page\Page;
|
||||
|
||||
class PagesProcessor extends ProcessorBase implements ProcessorInterface {
|
||||
|
||||
public $id = 'pages';
|
||||
@@ -17,6 +19,22 @@ class PagesProcessor extends ProcessorBase implements ProcessorInterface {
|
||||
$this->container['pages']->init();
|
||||
$this->container->fireEvent('onPagesInitialized');
|
||||
$this->container->fireEvent('onPageInitialized');
|
||||
|
||||
/** @var Page $page */
|
||||
$page = $this->container['page'];
|
||||
|
||||
if (!$page->routable()) {
|
||||
// If no page found, fire event
|
||||
$event = $this->container->fireEvent('onPageNotFound');
|
||||
|
||||
if (isset($event->page)) {
|
||||
unset ($this->container['page']);
|
||||
$this->container['page'] = $event->page;
|
||||
} else {
|
||||
throw new \RuntimeException('Page Not Found', 404);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
namespace Grav\Common\Service;
|
||||
|
||||
use Grav\Common\Page\Page;
|
||||
use Pimple\Container;
|
||||
use Pimple\ServiceProviderInterface;
|
||||
|
||||
@@ -77,14 +78,13 @@ class PageServiceProvider implements ServiceProviderInterface
|
||||
// Try fallback URL stuff...
|
||||
$c->fallbackUrl($path);
|
||||
|
||||
// If no page found, fire event
|
||||
$event = $c->fireEvent('onPageNotFound');
|
||||
|
||||
if (isset($event->page)) {
|
||||
$page = $event->page;
|
||||
} else {
|
||||
throw new \RuntimeException('Page Not Found', 404);
|
||||
if (!$page) {
|
||||
$path = $c['locator']->findResource('system://pages/notfound.md');
|
||||
$page = new Page();
|
||||
$page->init(new \SplFileInfo($path));
|
||||
$page->routable(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $page;
|
||||
|
||||
Reference in New Issue
Block a user