I am looking for a little assistance with a plugin. I have a page with a frontmatter property of ‘private_content’ and its set to true. I am trying to write a plugin that when a page is rendered, if private_content is set and if it is set to true, return the 404 page.
Code is as follows:
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
class ExamplePlugin extends Plugin
{
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
public function onPluginsInitialized($e)
{
$this->enable([
'onPageInitialized' => ['onPageInitialized', 0]
]);
}
public function onPageInitialized()
{
$page = $this->grav['page'];
$header = $page->header();
if( isset($header->private_content) && $header->private_content === true ){
$this->grav['page']->header()->http_response_code = 404;
$this->grav->fireEvent('onPageNotFound');
}
}
}