Jump to content

TemplatesChildPages


Robin S
 Share

Recommended Posts

GitHub: https://github.com/Toutouwai/TemplatesChildPages

For any page, allows the restricting of templates that may be used for child pages.

Usage

Install the TemplatesChildPages module. The module adds an AsmSelect inputfield "Template restrictions for children" on the Children tab of Page Edit. Use the inputfield to select from existing templates and any child pages added subsequently will be limited to those selected templates.

Note that any template restrictions added via this module are in addition to any "Family" restrictions set for the child and parent templates. In other words, if template Family restrictions prevent a template being selected for a given page you can't override this simply by including the template in the parent's "Template restrictions for children" field.

Why use this module?

The module allows you to add child page template restrictions without having to create new or duplicate templates for the parent page. This forum post explains the situation well.

Similarly, it makes it possible to use the "Add New" page feature without an unnecessary proliferation of templates. If you specify a single template as restriction for child pages you can add a Page Add Bookmark for that parent page that allows site editors to easily add a new page using the right template for the location.

 

module-1.png

module-2.png

 

Edited by Robin S
Updated info for v0.0.5
  • Like 11
Link to comment
Share on other sites

Hi Robin,

thanks for the module! just installed it without problems on 2.7.3

2 suggestions:

1) you are using wire(...) some times. for pw 3 i think it would be better using $this->wire(...); see here: https://processwire.com/blog/posts/processwire-2.6.21-upgrades-comments-more-on-pw-3.x/#more-updates-on-processwire-3.0

2) why does one have to add the field manually? i think the field could be added to every template automatically. it would even be better to not see the field in the template editor. don't know how or where you would save the value then... maybe that's the reason for your approach? :)

PS: any ideas why my field looks like this?

post-2137-0-28522900-1464259165_thumb.pn

Link to comment
Share on other sites

Thanks for the feedback :).

The faulty field rendering was due to the fact that the Children tab loads via AJAX, so AsmSelect fields contained there don't get to attach their JS/CSS files in the document head in the render() method as they normally would. Have worked around this so should be fixed now in v0.0.3

I should have said in the first post that this module is only really good as a stopgap measure until either:

a) this functionality is built into the core, as I think it should be

b) someone builds a better module for this :)

My dev skills are beginner/middling at best. I built this module for mostly for my own use and as a learning exercise. It gets the job done but I don't doubt for a second that there are better solutions possible.

Regarding your suggestions:

1. Not sure this is a major problem as I think only multi-instance PW 3.0 sites are affected. But it was simple to change wire() to $this so went ahead and did that.

2. I have changed the template storage field to global. Not 100% sure this is the way to go so would like to hear more opinions on this.

As for whether it is correct or not to use a fieldtype for storing the data - I have mixed feelings about this. I get where you're coming from and part of me does think it's a bit hack-ish. But on the other hand, the data stored in the field 'belongs' to the page so in many ways a fieldtype is ideal for this. I guess it depends if you believe a fieldtype should only ever hold page content and never page metadata. An example of another module that holds metadata in fieldtypes is Lumberjack so it's not without precedent.

In any case, I don't have a good enough knowledge of MySQL to create a dedicated table and database interface in this module. Bitpoet does this in his TemplateParents module and maybe once I've learned a bit more about MySQL I could do something similar but for now I think this module will stick with the fieldtype approach.

  • Like 1
Link to comment
Share on other sites

v0.0.5 released - a major reworking of the module.

  • TemplatesChildPages now saves template restrictions in a custom database table.
  • The module no longer has a dependency on the Template ASM Select module.
  • The module no longer adds a global field to templates.

Much credit is owed to the Template Access by Parents module by  @BitPoet , which provided the basis for the SQL additions.

  • Like 3
Link to comment
Share on other sites

  • 3 months later...
9 hours ago, Ivan Gretsky said:

What is the API way of checking, which templates are allowed for the page?

// Array of template IDs that are allowed for children. Blank array indicates "any"
$page->parent->template->childTemplates
// Array of template IDs that are allowed for parents. Blank array indicates "any"
$page->template->parentTemplates

...as per notes in core files...

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...
On 26/12/2016 at 11:48 PM, Ivan Gretsky said:

What is the API way of checking, which templates are allowed for the page?

@Ivan Gretsky, did kongondo's reply solve your issue?

If you mean the additional template restrictions added by TemplatesChildPages, I didn't include a method for returning the allowed templates as the module is only intended as an admin helper (and it only autoloads on admin pages). If you still have a need to get the module's template restrictions for a given parent via the API let me know and I'll look at adding a method for that purpose.

  • Like 1
Link to comment
Share on other sites

  • 5 years later...

Is there actually another possibility other than this module to predefine templates for pages (or defined parents/templates)?

I need exactly this function, but there have been no updates for this module for a long time 😉 

Link to comment
Share on other sites

4 hours ago, zoeck said:

there have been no updates for this module for a long time

That's not always a bad thing - if it ain't broke, don't fix it :)

There is the module by bitpoet that works in reverse (I think - I have used that one, but not this one).

  • Like 2
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...