Jump to content

[solved] Advanced Template Family Problem


horst
 Share

Recommended Posts

Hi,

I have a situation where I use settings in my templates that only ONE template sitewide can exist, or (via a module) for some templates I select only "One Per Parent". But this is not enough for me. ?

home-|
     |-basic-a-|
     |         |-sub-|
     |               |
     |
     |-basic-b
     |         |-sub-|
     |               |-specialOnlyOne-And-inSubFrom-basic-b
     |
     |-basic-c
     |         |-sub-|
     |               |
     |

This page-tree above is my setup: basic-a, basic-b, basic-c are different. SUB is all the same.

Is it possible to create a specialTemplate, that is allowed only One page sitewide, but also should NOT available in the template DropDownSelect for new pages, if the page is not a child from sub under basic-b?

Link to comment
Share on other sites

I know you asked for a configuration but, maybe hook here?

<?php
$wire->addHookAfter('ProcessPageAdd::getAllowedTemplates', function($e){
  $templates = $e->return;
  $specialTemplate = wire('template')->get('specialOne');
  $parent = $pages->get(wire('input')->get->parent_id);

  if($parent->template != "sub"){
	unset($specialTemplate->id, $templates);
    $e->return = $templates;
  }
});

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...

Hi @elabx,

thanks for the suggestion. I finally came up with a solution combining the hook and special names in the template flags. So with my example from above, I add a tag with the name allowedonly4-basic-b to the template that should be allowed only in the sub directory of basic-b, but not in other sub directories.

$wire->addHookAfter('ProcessPageAdd::getAllowedTemplates', function($event) {
    $pages = wire()->pages;
    $parent = 'sub' == $pages->get(wire('input')->get->parent_id)->template->name ? $pages->get(wire('input')->get->parent_id)->parent() : new NullPage();
    if(0 == $parent->id) return;
    $templates = $event->return;
    foreach($event->return as $template) {
        foreach(explode(' ', $template->tags) as $tag) {
            if('allowedonly4-' == substr($tag, 0, 13) && substr($tag, 13) != $parent->template->name && isset($templates[$template->id])) {
                unset($templates[$template->id]);
            }
        }
    }
    $event->return = $templates;
});

With this combination I now can create templates allowed only for one or two defined sub directories.

  • 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
 Share

  • Recently Browsing   0 members

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