Jump to content

Selector Fieldtype uses template id rather than name


FlorianA
 Share

Recommended Posts

In order to hide some pages from my site menu, I've defined a field called 'menu_filter' with field type 'Selector'. In my page editor, I assigned a value to the field by choosing the combo boxes 'template' 'Equals' 'event'.

After that I tried to apply this selector to a WireArray in a template file, but the selector string was 'template=45' rather than 'template=event', that means it shows the template's id, not its name. Therefore the selector doesn't work in my code. How can I fix this?

Link to comment
Share on other sites

19 hours ago, Macrura said:

what is your code?

 
function getSettings() {
    return pages()->get('/site-settings/');
}

$home = $pages->get('/');
foreach ($home->children()->not(getSettings()->menu_filter) as $childPage) {
    // the not() part has no effect
}

 

Link to comment
Share on other sites

23 hours ago, FlorianA said:

After that I tried to apply this selector to a WireArray

The problem is here. The description of InputfieldSelector/FieldtypeSelector is "Build a page finding selector visually" (my emphasis). The inputfield returns a string that is suitable for using with the PW PageFinder - so you can use it with methods that query the database such as $pages->find($selector) or $page->children($selector) but it isn't guaranteed to work with in-memory selectors like $some_pagearray->find($selector), $some_pagearray->not($selector), etc.

In your case a simple solution would be to use your Selector field to define the selector for pages that will appear in the menu (rather than what you don't want to appear in the menu).

So you would set this in your Selector field...

template > Not Equals > event

...and then use it in your template like this...

function getSettings() {
    return pages()->get('/site-settings/');
}

$home = $pages->get('/');
foreach ($home->children(getSettings()->menu_filter) as $childPage) {
    // ...
}

 

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