Nils Wiere Posted November 15, 2018 Share Posted November 15, 2018 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 More sharing options...
Tom. Posted November 15, 2018 Share Posted November 15, 2018 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 More sharing options...
Nils Wiere Posted November 15, 2018 Author Share Posted November 15, 2018 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 More sharing options...
LostKobrakai Posted November 23, 2018 Share Posted November 23, 2018 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) ); Link to comment Share on other sites More sharing options...
Nils Wiere Posted November 23, 2018 Author Share Posted November 23, 2018 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: Link to comment Share on other sites More sharing options...
Robin S Posted November 23, 2018 Share Posted November 23, 2018 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: 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(). 3 Link to comment Share on other sites More sharing options...
Nils Wiere Posted December 16, 2018 Author Share Posted December 16, 2018 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… 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now