Jump to content

is it possible to limit the level of child page creation for a template ?


adrianmak
 Share

Recommended Posts

I can't write a working example yet (on mobile) but if you're comfortable with hooks, you can hook into Page:addable and return false (change $event->return) or throw error when the page ($event->object)  has more than 2 parents ($page->parents->count) 

 

  • Like 4
Link to comment
Share on other sites

put this in your ready.php

wire()->addHookBefore('Page::addable', null, function ($e) {
   if ($e->object->parents->count > 1) $e->object->template->noChildren = 1;
});

 

  • Like 4
Link to comment
Share on other sites

  • 3 months later...

I was looking for this for this use case:

 

home

- news (news template)

- - local (news template)

- - - news item 1 (post template)

- - - news item 2 (post template)

- - - news item 3 (post template)

 

Posts under 'local' should use the 'post' template; 'news' template should not be allowed as an option.

kixe's solution looked promising, but disables any new child page creation.

Is there a way to limit the number of page levels by template - still allowing new pages with another template? 

Or is there a way to set a default child template at the page level?

Edit:

Or is the way to do this to temporarily allow 'news' to be used as child template, create those subsections, then lock those pages and allow only 'post' template to be used for next levels?

Changing the allowed template settings doesn't seem to affect previously created pages, which would be good. They stay on the selected template. Correct? Is that how it works?

And/or you create the sections and subsections on the 'news' template and then set 'Can this template be used for new pages' to 'no'?

Or can you control the template options via access control? What is the difference between 'create pages' and 'add children' in access settings on the template?

 

Link to comment
Share on other sites

  • 4 years later...
On 5/12/2017 at 3:23 AM, kixe said:

put this in your ready.php

wire()->addHookBefore('Page::addable', null, function ($e) {
   if ($e->object->parents->count > 1) $e->object->template->noChildren = 1;
});

 

I just used this today – very handy. I specified a template too:

wire()->addHookBefore('Page::addable', null, function ($e) {
	if ($e->object->parents("template=basic-page")->count > 1) $e->object->template->noChildren = 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...