Jump to content

Prohibit 'view' of pages with certain template


didhavn
 Share

Recommended Posts

Hey all.

I have a question concerning templates and a possible setting to disallow/prohibit to view pages that are using certain templates.

I have a couple of templates that are not complete page-templates including header/footer but are sections-templates. E.g. I have a template called 'Case' and every page using this template can add section children, e.g. first a section-video, then section-textblock, ...

Doing this, the user can create its own cases out of building blocks.

However, up to now one can select 'view' in the page-tree in the admin panel to view each sections. This view failes, as they don't have a header/footer.

Can anyone of you think of solution how to disable that one can view pages with template section-* ?

Thanks a lot in advance!

Link to comment
Share on other sites

Move all section template files in a folder "sections" and include them like that for rendering in other templates:

$markup = wireRenderFile("sections/section-article", array( 'page' => $pageToRenderOf ));

Because the files are no longer stored at the default template path these pages do no longer show up as viewable. 

  • Like 6
Link to comment
Share on other sites

Another approach would be a hook in your init.php

$this->addHookAfter('Page::viewable', function($event) {
    if(strpos($event->object->template, 'section-') !== false) {
        $event->return = false;
    }
});
  • Like 3
Link to comment
Share on other sites

Hey LostKobrakai and adrian.

Thanks a lot for your suggestions.

The solution with the hook would be very elegant, unfortunetly I can not get it working.

I placed the code-snipped in _init.php and all my section-templates do indeed start with section-* thus in principal it should work like proposed.

I have little experience with hooks but I think the hook is not called at all because e.g. a $this->message('hook called') does not work.

Any suggestions?

Link to comment
Share on other sites

The init.php file needs to be: /site/init.php

Have a read about this here: https://processwire.com/blog/posts/processwire-2.6.7-core-updates-and-more/#new-core-files-for-site-hooks

PS - the hook would also work in /site/templates/admin if you only want to prevent the view link in the admin. If you want to prevent separate viewing on the front-end of the site as well, then it should be in /site/init.php

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...