Filtering module subpages

I’m using the modules plugin and I want to filter the module subpages to not be shown in a submenu.

This is my approach:

$children = $item->children()->visible()->filterBy('template', '!=', 'module.name1')->filterBy('template', '!=', 'module.name2' )->filterBy('template', '!=', 'module.name3' )...;

Is there a simpler way to do it?

This should work:

$children = $items->children()->visible()->filterBy('template', '*=', 'module.');

If not, use a filter with callback and check if the string starts with module

It works! (adding the exclamation mark !*=).

Thanks Sonja.

Oh, yes, of course, doing too many things at the same time right now.

Focus, focus… :slight_smile:

1 Like

I thought it worked, but it doesn’t return any page.

I found another solution in Filtering compendium:

$children = $item->children()->visible()->filterBy('template', 'not in', ['module.name1', 'module.name2',...]);

In this way, I do not have to repeat filterBy for each template.

It could be even better if I could indicate module.* just once.

What exactly. are you trying to fetch, what is $items. in your example above? It should be a single page but seems to be a collection of pages?

It’s a nested menu.

But then your variable should be $item->children()->visible()->filter() not $items->children()->etc()

Yes, I already changed it, in my last example is as you say.

Ok, I. think the problem is the template filter, a module doesn’t have template, should work if you use intendedTemplateinstead:

$modules = $item->children()->filterBy('intendedTemplate', '*=', 'module');
$children = $item->children()->not($modules);

As far as I can see, there is no !*= operator?

It seems to work with template too.

The key was the operator, using your code now it works.