Jump to content

Template Parents


BitPoet
 Share

Recommended Posts

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.

post-2900-0-07912600-1458482835_thumb.pn

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.

post-2900-0-97368900-1458482883_thumb.pn

The module can be downloaded from the GitHub repository. It has been tested with PW 2.7 and 3.0.

  • Like 15
Link to comment
Share on other sites

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

post-2900-0-91562000-1458494320_thumb.pn

  • Like 9
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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 :biggrin:

  • Like 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

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. :)

  • Like 1
Link to comment
Share on other sites

  • 6 years later...

...

Edited 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*
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...