Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Conditional Frontmatter Filtering

Started by Jamie Pickett 6 years ago · 2 replies · 454 views
6 years ago

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
    <?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');          
        }
    }
}
6 years ago

@pickettj, Would you mind sharing what the issue is you are trying to solve?

It sounds like this should be solved with the build-in authorisation settings.

6 years ago

It is an authorization type of thing, but I am not planning on using the built in login plugin. I am also not looking to create 30K+ user yaml files to control access to content.

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 54 1 week ago
Plugins · by Xavier, 4 weeks ago
2 61 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1188 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 54 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 79 2 months ago