Jump to content

Template Once Per Parent


BitPoet
 Share

Recommended Posts

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.

topp.png.bab0e1e38e42f963d640a398b7c0fc53.png

Edited by BitPoet
Added link to module repository
  • Like 7
Link to comment
Share on other sites

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.

  • Like 2
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

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) {

 

Link to comment
Share on other sites

+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;
        });

 

Link to comment
Share on other sites

  • 6 years later...

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • 4 months later...

@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!

  • Thanks 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...