Refactored onPageNotFound event to fire after onPageInitialized

This commit is contained in:
Andy Miller
2016-09-08 12:30:34 -06:00
parent da0dbeb6b3
commit b6e785bd2a
3 changed files with 30 additions and 7 deletions

5
system/pages/notfound.md Normal file
View File

@@ -0,0 +1,5 @@
---
title: Not Found
routable: false
notfound: true
---

View File

@@ -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);
}
}
}
}

View File

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