Skip to content

Commit

Permalink
Refactored onPageNotFound event to fire after onPageInitialized
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Sep 8, 2016
1 parent da0dbeb commit b6e785b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
5 changes: 5 additions & 0 deletions system/pages/notfound.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Not Found
routable: false
notfound: true
---
18 changes: 18 additions & 0 deletions system/src/Grav/Common/Processors/PagesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Grav\Common\Processors;

use Grav\Common\Page\Page;

class PagesProcessor extends ProcessorBase implements ProcessorInterface {

public $id = 'pages';
Expand All @@ -17,6 +19,22 @@ public function process() {
$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);
}
}

}

}
14 changes: 7 additions & 7 deletions system/src/Grav/Common/Service/PageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Grav\Common\Service;

use Grav\Common\Page\Page;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

Expand Down Expand Up @@ -77,14 +78,13 @@ public function register(Container $container) {
// 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;
Expand Down

0 comments on commit b6e785b

Please sign in to comment.