Jump to content

Selector operator: Word/phrase DOESN'T appear at start/end of compared value (extension of the `!=` operator)


SwimToWin
 Share

Recommended Posts

Request to have a selector operator that allows me to filter out results that do not begin with a specified comparison value. For instance like so:

  • `^!=`
  • `$!=` (nice to have-addition)
  • ... think of it as an extension of the `!=` operator.

Consider these use cases:

In the Admin Page Editor I want to show the field "Summary" only when the page path doesn't start with "/foo/" (preferably not using LIKE to do the comparison). Selectors don't allow checking the root parent for a path (as I recall, that used to be possible, I might be wrong though). Filtering out the field might be done with these Selector operators:

  • path^!=/foo/
  • path^!=/foo/|/bar/|/baz/

Result:

  • Page path: /foo/ and /foo/one/two/
    • "Summary" field is not shown.
  • Page path: /bar/ and /bar/one/
    • "Summary" field is shown.

See also the Selectors documentation

https://processwire.com/docs/selectors/
https://cheatsheet.processwire.com/selectors/

  • `^=` - Starts with phrase/text
    • Word or phrase appears at start of compared value.
  • `$=` - Ends with phrase/text
    • Word or phrase appears at end of compared value.
  • `^!=` - Starts doesn't with phrase/text (if/when this request is implemented)
    • Word or phrase doesn't appears at start of compared value.
  • `$!=` - Ends doesn't with phrase/text (if/when this request is implemented)
    • Word or phrase doesn't appears at end of compared value.
Link to comment
Share on other sites

51 minutes ago, SwimToWin said:

Request to have a selector operator that allows me to filter out results that do not begin with a specified comparison value.

Generally speaking this is already possible. The syntax is:

!your_field_name^=foo

Paths are a special case though because these are only searchable when the PagePaths module is installed and that module doesn't support this kind of negation:

 2021-08-07_200050.png.1da0bf9d8a0f6f63facb15a505c83c46.png

But I don't think this actually matters because there is a dedicated selector option for when you want to find pages that do or do not have a particular ancestor, and that is "has_parent": https://processwire.com/docs/selectors/#finding2

So rather than needing anything like...

1 hour ago, SwimToWin said:

path^!=/foo/|/bar/|/baz/

...you can instead do...

has_parent!=/foo/|/bar/|/baz/

 

  • Like 2
Link to comment
Share on other sites

  • 2 years later...
On 8/7/2021 at 9:05 AM, Robin S said:

So rather than needing anything like...

On 8/7/2021 at 8:04 AM, SwimToWin said:

path^!=/foo/|/bar/|/baz/

...you can instead do...

has_parent!=/foo/|/bar/|/baz/

Long time ago, but another gem from @Robin S!

  • Like 1
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...