BitPoet Posted March 20, 2016 Posted March 20, 2016 A feature request here in the forums prompted me to shape a piece of code I had been tinkering with into a usable module. Template Parents What it does: The list of possible templates for new pages in ProcessWire is purely based on other templates in parent/child relationships managed in the involved templates' family settings. This is most often okay, but sometimes you want to limit creation of pages with a certain templates to individual spots in the page tree, and there's no clear parent/child relationship to go with. The initially quick solution would be to duplicate parent templates for the different spots, but then any change on one of these templates has to be made to the others too, which gets tedious and error prone. Template Parents adds an entry in the Setup menu where you can assign allowed parent pages to templates. These rules are enforced after the regular family settings have been executed, so it builds upon instead of replacing PW's built-in mechanism. There's also an option in ProcessTemplateParents' to enable inheritance, then entries in the Template Parents assignment are inherited down the page tree and also grant template permissions to children, grandchildren and so on. The module can be downloaded from the GitHub repository. It has been tested with PW 2.7 and 3.0. 15
Ivan Gretsky Posted March 20, 2016 Posted March 20, 2016 Great thing! And your reaction speed to forum requests is fascinating! I guess it might not be too hard to integrate this into the template family tab. This way working with parent/child relationships would be easier - you do not have to go to 2 different screens for that. What do you think? 2
BitPoet Posted March 20, 2016 Author Posted March 20, 2016 @Ivan: A good idea. In fact, the one I initially had in mind, but I somehow got it into my head to save the settings directly with the template configuration which I couldn't. Of course, one thing doesn't necessitate the other and I can both hook into ProcessTemplate and use my own database table. So, version 0.0.6 is on GitHub and does allow that. 9
Robin S Posted March 20, 2016 Posted March 20, 2016 BitPoet, many thanks for this module! When attempting to go to the settings page for Template Parents Process in PW v2.7.2 I get an error:"Undefined index: inheritSettings in ...\site\modules\TemplateParent\ProcessTemplateParents.module on line 232" Also, do you think there's a way to have templates that have been given a parent page restriction with this module appear in the "Add New" menus?
BitPoet Posted March 21, 2016 Author Posted March 21, 2016 There was a silly mistake in the module config, but that should be fixed in the latest version on github. Currently, the only way to get the template selectable once parent restrictions are in place is to add a valid parent to the settings. Can you describe a little more what kind of logic you have in mind?
Robin S Posted March 21, 2016 Posted March 21, 2016 Currently, the only way to get the template selectable once parent restrictions are in place is to add a valid parent to the settings. Can you describe a little more what kind of logic you have in mind? I'm thinking of the same "Add New" functionality you get when you use the core "Allowed template(s) for parents" + "Allowed template(s) for children" + "Show in the add-page shortcut menu" options. It lets you add a new page of a particular template in one step because PW "knows" where in the tree a page of that template is allowed to go. In theory this functionality could also apply to templates with a parent page restriction defined with your module, but I'm not sure how difficult it would be to extend the Add New functions to allow for this. 1
Robin S Posted March 21, 2016 Posted March 21, 2016 Loving this module - perfect for a site I'm currently developing. Just noticed a bug: allowed parents selected from Setup > Templates > Edit Template are not saved successfully (no selection is saved). Allowed parents selected from Setup > Template Parents are saved okay. Another thought: how about a sister module for Template Parents, "Page Child Templates"? So for any page you can define allowed templates that may be used for child pages. Now I'm just being greedy 1
BitPoet Posted March 22, 2016 Author Posted March 22, 2016 I need to have a look what can go wrong with changes in template settings, it worked in my (limited) tests. Not sure I want to go into the page/child templates direction though, as the data model is currently tailored to be template-centric. Making both ways efficient would add a whole layer of complexity (n:m relationship).
SteveB Posted March 23, 2016 Posted March 23, 2016 Any ideas about how to get the one-step add Robin was asking about? Maybe add a setting in your module for name format. ...add a new page of a particular template in one step because PW "knows" where in the tree a page of that template is allowed to go Greedy of me to ask but you seem to be a couple steps ahead of me on this.
Robin S Posted March 23, 2016 Posted March 23, 2016 Any ideas about how to get the one-step add Robin was asking about? @BitPoet: Incidentally, one reason I thought a sister "Page Child Templates" module would be useful is that it avoids any potential difficulties there might be in extending the "Add New" feature (which normally requires allowed templates to be configured for both parent and child templates, although it's not clear to me why the parent template restriction on children is logically necessary for this feature). If it were possible to specify for a given page what template(s) may be used for child pages then the Page Add Bookmarks feature could be used in place of the original "Add New" feature. The Page Add Bookmarks feature just adds a page under a given parent in the tree, but if a "Page Child Templates" module has locked down allowed templates for that parent then you have a pretty foolproof substitute for "Add New" without having to add extra unnecessary templates for those parent pages. Making both ways efficient would add a whole layer of complexity (n:m relationship). My understanding of the relationships required is pretty limited, but looking at your code for the Template Parents module it looks like it could be adapted for a Page Child Templates module. The way I imagine it working is that for any page that has allowed templates for children configured, the module stores a row in a database table containing allowed template IDs for that page ID. Then when a new page is added, the filterAllowedParents function hooked to ProcessPageAdd::getAllowedTemplates filters the list of allowed templates according to any template IDs that are stored for the new page's parent page ID. But I might be overlooking some complexity here. I'll probably have a tinker with your Template Parents module to see if I can adapt it myself but I'm a bit of an amateur. 1
bernhard Posted May 25, 2016 Posted May 25, 2016 i started a new post in the wishlist section regarding this topic: https://processwire.com/talk/topic/13374-allowed-templates-on-a-page-basis/
Robin S Posted May 26, 2016 Posted May 26, 2016 Another thought: how about a sister module for Template Parents, "Page Child Templates"? So for any page you can define allowed templates that may be used for child pages. I have made a simple module for this: info here.
Robin S Posted May 28, 2016 Posted May 28, 2016 Just noticed a bug: allowed parents selected from Setup > Templates > Edit Template are not saved successfully (no selection is saved). Allowed parents selected from Setup > Template Parents are saved okay. Hi @BitPoet, I just created a GitHub pull request with a fix for this. Cheers. 1
Arcturus Posted March 3, 2023 Posted March 3, 2023 (edited) ... Edited March 3, 2023 by Arcturus Realized that the reason that saving changes worked in 3.0.184 and not 3.0.210 is that I read Robin's workaround at the time and then forgot about it when I needed it today. *sighs*
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