RobbieR Posted August 25, 2016 Share Posted August 25, 2016 Hello I run into issue with find function. Is there a way to use find removing page title spaces? Example: $page->title = "505 tt"; // looking for this page $keyword = "505tt"; $p = $pages->find('template=project, title~=$keyword'); It return no page. Is there a way to find right page? At the moment i get all pages and then do loop. Removing title spaces and compare. I think its over kill. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted August 25, 2016 Share Posted August 25, 2016 Variables in single quotes will not be processed. Use double quotes when using variables in selectors. 4 Link to comment Share on other sites More sharing options...
szabesz Posted August 25, 2016 Share Posted August 25, 2016 1 hour ago, RobbieR said: Is there a way to use find removing page title spaces? In addition to @Martijn Geerts's piece of advice, you might also need something like this: https://www.yourhowto.net/strip-spaces-string-php/ It's not really about find, but about working with strings. 3 Link to comment Share on other sites More sharing options...
BitPoet Posted August 25, 2016 Share Posted August 25, 2016 I don't think there's a straight-forward solution for this. Usually, advanced search requirements like this are covered by external search engines like Lucene, but those might a bit of overkill. A lean solution would be to create a (hidden) text field, let's call it titlenospace, and add it to all templates you want to search. Then, in site/ready.php (create the file if not there yet), hook into Pages::saveReady and populate the new field with the whitespace-stripped contents of the title field. <?php if($page->template == "admin") { wire()->addHookAfter("Pages::saveReady", function(HookEvent $event) { $page = $event->arguments(0); if($page->template->hasField("titlenospace")) { $page->titlenospace = str_replace(' ', '', $page->title); } }); } For a multi-language site, the titlenospace field has to be created as TextLanguage and the code gets slightly more verbose. <?php if($page->template == "admin") { wire()->addHookAfter("Pages::saveReady", function(HookEvent $event) { $page = $event->arguments(0); if($page->template->hasField("titlenospace")) { $page->titlenospace = str_replace(' ', '', $page->title); if(wire("languages")) { foreach(wire("languages") as $lang) { if(!$lang->isDefault()) { $page->setLanguageValue($lang, "titlenospace", str_replace(' ', '', $page->getLanguageValue($lang, "title"))); } } } } }); } The titlenospace field needs to be populated for existing pages (simply saving them should do). Then you can use the field in your selector instead of or in addition to the regular title field. 6 Link to comment Share on other sites More sharing options...
Roberts R Posted August 26, 2016 Share Posted August 26, 2016 @BitPoet Thanks for solution. It fits my needs quite well 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