BitPoet Posted January 16, 2018 Posted January 16, 2018 (edited) So I stumbled over the request to allow limiting templates to be used only once under every parent page in this thread and found that this would actually come in handy (also in a site I've built). The code can be found on github and soon also in the module repo. After installation, you'll find a new checkbox "Only once per parent" in the family tab when editing a template. Edited January 17, 2018 by BitPoet Added link to module repository 7
adrian Posted January 16, 2018 Posted January 16, 2018 Hi @BitPoet - love the response time on this Just a oversight I know, but can I make a suggestion that you please change: $parent->children("template={$template}")->count() > 0; to: $parent->count("template={$template}") > 0; The execution time on a parent with a lot of children is going to be a problem otherwise. 2
Robin S Posted January 16, 2018 Posted January 16, 2018 6 minutes ago, adrian said: The execution time on a parent with a lot of children is going to be a problem otherwise. That was my initial thought too, but then I realised that this module by definition applies to situations where there is only zero or one child of that template under a parent. So that selector is at most going to return one page.
adrian Posted January 16, 2018 Posted January 16, 2018 1 minute ago, Robin S said: That was my initial thought too, but then I realised that this module by definition applies to situations where there is only zero or one child of that template under a parent. So that selector is at most going to return one page. Right - I guess I was just worried what would happen if someone applied that setting to an existing template on their site accidentally. Maybe unlikely and not a big deal, but is there any reason not to change to my version? 1
BitPoet Posted January 17, 2018 Author Posted January 17, 2018 No reason at all, so 0.1.1 on github already has the change. Thanks for taking a look and pointing it out @adrian 2
tpr Posted February 18, 2018 Posted February 18, 2018 Hi, I remembered this module and now I needed it in a project. However, it seems that it cannot handle childrens with multiple type of templates. I would like to add 2 children pages per parent, "answer" and "review". When I add one, I cannot add the second one - so if I have added an "answer", I get "No templates allowed for adding new pages here." error when trying to add a "review". The parent template have both templates enabled as children templates. Here is a quick fix (about line 40), of course please review if needed: if($template->oncePerParent && count($parent->children("template={$template}")) > 0) { instead of if($template->oncePerParent && $parent->count("template={$template}") > 0) {
tpr Posted February 18, 2018 Posted February 18, 2018 +1: this removes the "New" action if there is no room for more children. I haven't tested it thoroughly so may have issues. Update: this hook doesn't allow to add new child page if all the allowed type of templates are already added. Use only if you need to restrict child pages to the allowed templates and allow no other templates. $this->addHookAfter('ProcessPageListActions::getActions', null, function(HookEvent $event) { $p = $event->arguments[0]; $actions = $event->return; $numChildrenWithOncePerParent = count($p->children()->find('template.oncePerParent=1')->template()); if($numChildrenWithOncePerParent > 0 && (count($p->template->childTemplates) <= $numChildrenWithOncePerParent)) { unset($actions['new']); } $event->return = $actions; });
cb2004 Posted April 10, 2024 Posted April 10, 2024 Unfortunately this doesn't seem to work with the current version of ProcessWire 3.0.237. As soon as I add 1 page with a template that is restricted, it then cant be added anywhere else, even way out of the way of the other parent.
BitPoet Posted April 15, 2024 Author Posted April 15, 2024 On 4/10/2024 at 4:23 PM, cb2004 said: Unfortunately this doesn't seem to work with the current version of ProcessWire 3.0.237. As soon as I add 1 page with a template that is restricted, it then cant be added anywhere else, even way out of the way of the other parent. Can you try the latest release (0.2.0) and let me know if that fixed the issue?
cb2004 Posted April 15, 2024 Posted April 15, 2024 @BitPoet it certainly did, thank you for the quick response. 1
FireWire Posted August 27, 2024 Posted August 27, 2024 @BitPoet Great module! Just saved me time and was exactly what I was looking for. The one thing I ran into is that if a child page with a single-template child restriction is set to hidden then it's still possible to select that template when creating a new page. My use case is a settings page that is published but hidden. I tested the changes made in this Github PR and it resolves the issue. Thanks again for the module! 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now