JayGee Posted June 11, 2024 Posted June 11, 2024 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?
wbmnfktr Posted June 11, 2024 Posted June 11, 2024 What about excluding all pages that have the parent with ID 2 - as that would be the admin? pages->find('!hasParent=2, template=all|my|templates') 2
Robin S Posted June 11, 2024 Posted June 11, 2024 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-". 3
JayGee Posted June 12, 2024 Author Posted June 12, 2024 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! ?♂️ 1
JayGee Posted June 12, 2024 Author Posted June 12, 2024 (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 June 12, 2024 by JayGee Results of testing added. 1
wbmnfktr Posted June 12, 2024 Posted June 12, 2024 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. 1
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