Juergen Posted January 19, 2023 Share Posted January 19, 2023 Hello @ all Is there a possibility to filter a TemplatesArray by the selector childTemplates, which has an array as value? I have searched the net, but I have not found a solution. The problem seems to be that the value is an array and not a string. This does not work: $templates->find('childTemplates=[29,30]); This does not work too: $templates->find('childTemplates=29|30'); Has anyone an idea? Link to comment Share on other sites More sharing options...
wbmnfktr Posted January 19, 2023 Share Posted January 19, 2023 Just an idea based on similar queries I used in the past... totally untested for your use-case. $templatesFromPages = new PageArray(); // we go from here foreach($pages->find("template=news|events|otherTemplates") as $myPages) { // add template from found pages $templatesFromPages->add($myPages->template->name); }; With this you might be able to look for this: $templates->find("name=$templatesFromPages"); 1 Link to comment Share on other sites More sharing options...
BitPoet Posted January 19, 2023 Share Posted January 19, 2023 1 hour ago, Juergen said: The problem seems to be that the value is an array and not a string. It definitely is. WireArray::filterData, which is responsible for executing the selector, does an explicit cast to (string) on the value. So it ends up with a literal "Array" instead of the values in childTemplates. This has the funny side effect that $templates->find('childTemplates=Array'); returns all templates. I'd call it a bug, since filterData seems to assume that all array-like values stringify to a pipe separated list like WireData, yet Selector::matches can deal with array values just fine. But since templates are loaded into memory anyway, you can just iterate over them and do a manual comparison without a performance or memory penalty. <?php namespace ProcessWire; $matched = []; foreach($templates as $t) { if( count(array_intersect($templates->childTemplates, [29,30])) ) $matched[] = $t; } 4 Link to comment Share on other sites More sharing options...
Juergen Posted January 20, 2023 Author Share Posted January 20, 2023 Thank you @wbmnfktr and @BitPoet for your suggestions. I have forgotten to write, that I have a working solution by using a foreach loop, but my goal was to get rid of the loop and to use a selector instead. I only wanted to simplify the code to increase the performance. 8 hours ago, BitPoet said: WireArray::filterData, which is responsible for executing the selector, does an explicit cast to (string) on the value. This is the reason and therefore there is no possiblity to use a selector in this case, so I close this thread and use the loop 🙁 1 Link to comment Share on other sites More sharing options...
Robin S Posted January 20, 2023 Share Posted January 20, 2023 I created a GitHub issue: https://github.com/processwire/processwire-issues/issues/1669 2 Link to comment Share on other sites More sharing options...
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