Jump to content

How to filter templates by selector childTemplates [SOLVED]


Juergen
 Share

Recommended Posts

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

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");

 

  • Like 1
Link to comment
Share on other sites

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;
}

 

  • Like 4
Link to comment
Share on other sites

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 🙁

  • Like 1
Link to comment
Share on other sites

  • Juergen changed the title to How to filter templates by selector childTemplates [SOLVED]

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