Nils Wiere

Sanitizing Selectors

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

Share this post


Link to post
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) );

 

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

Either match by the id or match by the title.

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

Share this post


Link to post
Share on other sites

Hello,

didn't think about matching the id's – that works perfectly. Thanks!

However, matching the title still doesn't work when using an "&" in the title. As soon as I exchange the character it works:

selector-issue.gif.361fa6052e13e8e02282e0287cacb692.gif

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Hello Robin, thanks for bringing light to this issue. The entity encoder textformatter really didn't come to my mind here. Now it makes perfect sense…

 

  • Like 2

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.