Jump to content

Sanitizing Selectors


Nils Wiere
 Share

Recommended Posts

Hello,

this must be an easy one, however, I can’t get the selector to work.

I have a brand page where the brand’s name is the title of the page. I then find all products that have this brand assigned through a page reference field. That works fine using this code:

$brand = $page->title;

// brand is the page reference field
$products = $pages->find("brand.title");

However, whenever there's a special character like an ampersand in the brand's name, it doesn't return any results. I know how use sanitizer for the $brand variable. But then I also need to sanitize the selector so it matches the sanitized $brand variable, right? How would I do that?

Thanks,
Nils

Link to comment
Share on other sites

Hi @Nils Wiere

If that is the exact code, there is a few issues. 

$pages->find("brand.title"); 

I believe will return any page that has a field brand assigned to it. You aren't referring to a specific template. You are also not telling it anything to search. I believe the code you are looking for is as follows.

$brand = $page->title;
$products = $pages->find("template=brands, brand.title=" . $sanitizer->selectorValue($brand) );

// OR If you want to avoid searching within a template.

$products = $pages->find("brand.title=" . $sanitizer->selectorValue($brand) );

 

Link to comment
Share on other sites

Hi Tom,

Thanks for the quick reply! I shortened the code a bit for the forum and it was meant to be:

$pages->find("brand.title=$brand");

But yes, that's what I am looking for and using selectorValue works with many special characters, but it still doesn't work with the &-character in the page title. As soon as I change the ampersand to "and" I get the expected results.

I also tried it with a clean installation of ProcessWire (3.0.118) without the expected results:

$brand = $page->title; // page title is "A & B"

// find pages where page reference field brand is set to "A & B"
$products = $pages->find("brand.title=A & B"); // works
$products = $pages->find("brand.title=$brand"); // doesn't work
$products = $pages->find("brand.title=" . $sanitizer->selectorValue($brand) ); // doesn't work

 

Link to comment
Share on other sites

  • 2 weeks later...

I think this might be one of those cases where a little debugging saves a lot of head-scratching.

Try dumping the value you are trying to match:

2018-11-24_094512.png.18dbc746ce79bc7420ef210b8e0d712a.png

Now it's obvious what the cause of the issue is. The title field has the entity encoder textformatter applied (like all text fields should), so the formatted value that is prepared for display on the front-end is different than the unformatted value that is saved in the database and that you are searching against with $pages->find().

2018-11-24_094738.png.fb9012e4c7ba9feb611e2c43ae1d82a1.png

 

  • Like 3
Link to comment
Share on other sites

  • 4 weeks later...

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