Jump to content

Best way to find non admin templates?


JayGee
 Share

Recommended Posts

This feels like it should be simple but can't see it in the API docs.

I want to loop through all a site's templates and add some fields via a module, but I don't want to touch admin templates.

What's the best way to filter them? Is there a selector for $templates->find() like admin=false or similar?

Link to comment
Share on other sites

System templates have the Template::flagSystem flag.

So the "proper" way:

$non_system_templates = new TemplatesArray();
foreach($templates as $template) {
    // Skip templates with the system flag
    if($template->flags & Template::flagSystem) continue; 
    $non_system_templates->add($template);
}

The lazy way that is likely to work 99.9% of the time:

$non_system_templates = $templates->find("flags=0");

For some reason templates that are used for the custom fields for files/images feature don't have the system flag, so you will have to exclude those separately if you have any. The name of such templates starts with "field-".

  • Like 3
Link to comment
Share on other sites

Thanks both, this all makes sense - I knew there would be a way I was overlooking!

I think in all likelihood I'll use a combination of the 2 suggested selectors as there's no reason for me to touch anything in the admin folder:

$non_system_templates = $templates->find("flags=0,!hasParent=2");

I need to make sure I also exclude repeater templates. The reason this issue came up is because I'm adding a repeater field, and it got added to the repeater templates which caused an infinite loop when adding a repeater item! ?‍♂️

  • Like 1
Link to comment
Share on other sites

Posted (edited)
49 minutes ago, JayGee said:

Thanks both, this all makes sense - I knew there would be a way I was overlooking!

I think in all likelihood I'll use a combination of the 2 suggested selectors as there's no reason for me to touch anything in the admin folder:

$non_system_templates = $templates->find("flags=0,!hasParent=2");

I need to make sure I also exclude repeater templates. The reason this issue came up is because I'm adding a repeater field, and it got added to the repeater templates which caused an infinite loop when adding a repeater item! ?‍♂️

On second thoughts - hasparent is a page selector isn't it rather than a template selector?

 

EDIT:

Can confirm this works perfectly for my scenario - thanks for your help @wbmnfktr and @Robin S:

$templates = $this->templates->find("flags=0");

Now I know of the existence of the template flag system this is really handy.
For reference for anyone readying this in the future, checking the database I can see the standard templates all have flag 0 as implemented above. The admin repeater templates are all flag 8.

Edited by JayGee
Results of testing added.
  • Like 1
Link to comment
Share on other sites

3 hours ago, JayGee said:

On second thoughts - hasparent is a page selector isn't it rather than a template selector?

That's correct.

I would look for all pages and after that check for their templates and take those to modify. Different route and probably not the best way to do it - compared to @Robin S solution.

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