Jump to content

Restrict templates of children by parent


Recommended Posts

Being able to restrict the templates used for children pages under a template's Family tab has always come in handy (defined under the "Allowed templates for children" setting), but is there a way to restrict the templates used for children pages for a specific parent page (based on ID) rather than the parent page's template?

My use case: I have a template called "Data Container" which I use for several pages to keep pages organized.  Each of these data containers have children of specific template types.  For instance, one data container might have "cities" and another may have "buildings", etc.  Currently, an admin user has the freedom to choose any template type for children of any Data Container page.  

While I could create a new template for each data container that restricts children by template, it seems redundant because every one of those data container templates only has a "title" field and they will always only be used for one page. I think it would be great if I can define a page's children's template type by the specific page itself (based on the page's ID).  That way, I can only have one Data Container template but restrict an admin from putting a child with an incorrect template in the appropriate data container.

Link to comment
Share on other sites

Yep possible, what I sometime like to do is prefixing my template names. For example:

$templates = $this->templates->find("name^=element_"); and then returning all templates starting with element_.

This will save me from 'forgetting' to set family settings & i'm lazy :-)

You could write an autoload module and Hook to:

$this->addHookBefore('ProcessPageAdd::getAllowedTemplates', $this, 'beforeGetAllowedTemplates');
public function beforeGetAllowedTemplates(HookEvent $event) {

    $parent_id = $this->wire('input')->get('parent_id');
    $parent = $this->pages->get($parent_id);
    if (!$parent->id) return;

    // As it is a before hook, replace it
    $event->replace = true;

    $array = array();
    // All templates starting with name element_ for example
    $elements = $this->templates->find("name^=element_");
    foreach ($elements as $template) {
        // Do your dingdingdong
        $array[$template->id] = $template;
    $event->return = $array;
  • Like 8
Link to comment
Share on other sites

Ah. Okay, so Martijn's solution won't work for me.

I suppose the implementation I imagine something like this would work is that under a page's "Settings" tab, there is the same configuration setting that we see on a template's "Family" tab that allows us to define what templates the children of this page can use.

Given PW's core functionality, would adding something like this by using a module be possible - and if so, would clicking "Add New" be able to take into account the parent page's allowed templates for children?

  • 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

  • Recently Browsing   0 members

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