Hani Posted April 16, 2015 Share Posted April 16, 2015 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 More sharing options...
Martijn Geerts Posted April 16, 2015 Share Posted April 16, 2015 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; } 8 Link to comment Share on other sites More sharing options...
Hani Posted April 16, 2015 Author Share Posted April 16, 2015 Beautiful! Thanks, Martijn! 1 Link to comment Share on other sites More sharing options...
Soma Posted April 16, 2015 Share Posted April 16, 2015 Just wanted to mention that this method doesn't allow to use the "Add new" page feature which is based on the family settings of a template. Link to comment Share on other sites More sharing options...
Hani Posted April 16, 2015 Author Share Posted April 16, 2015 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? 1 Link to comment Share on other sites More sharing options...
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