Last active
March 15, 2021 08:55
-
-
Save baukezwaan/266c469758319daea5460f2a5647c525 to your computer and use it in GitHub Desktop.
Fix for issue with Fluent search in SilverStripe 4 (thanks @hubertusanton)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
Name: fluentsearchfix | |
--- | |
SilverStripe\Core\Injector\Injector: | |
SilverStripe\CMS\Search\SearchForm: | |
class: Hamaka\Extensions\SearchFormFluentFixExt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Hamaka\Extensions; | |
use SilverStripe\CMS\Model\SiteTree; | |
use SilverStripe\CMS\Search\SearchForm; | |
use SilverStripe\ORM\ArrayList; | |
use SilverStripe\ORM\DB; | |
use SilverStripe\ORM\SS_List; | |
use TractorCow\Fluent\State\FluentState; | |
class SearchFormFluentFixExt extends SearchForm | |
{ | |
/** | |
* Return dataObjectSet of the results using current request to get info from form. | |
* Wraps around {@link searchEngine()}. | |
* | |
* @return SS_List | |
*/ | |
public function getResults() | |
{ | |
// Get request data from request handler | |
$request = $this->getRequestHandler()->getRequest(); | |
$keywords = $request->requestVar('Search'); | |
$andProcessor = function ($matches) { | |
return ' +' . $matches[2] . ' +' . $matches[4] . ' '; | |
}; | |
$notProcessor = function ($matches) { | |
return ' -' . $matches[3]; | |
}; | |
$keywords = preg_replace_callback('/()("[^()"]+")( and )("[^"()]+")()/i', $andProcessor, $keywords); | |
$keywords = preg_replace_callback('/(^| )([^() ]+)( and )([^ ()]+)( |$)/i', $andProcessor, $keywords); | |
$keywords = preg_replace_callback('/(^| )(not )("[^"()]+")/i', $notProcessor, $keywords); | |
$keywords = preg_replace_callback('/(^| )(not )([^() ]+)( |$)/i', $notProcessor, $keywords); | |
$keywords = $this->addStarsToKeywords($keywords); | |
// $results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", "", $booleanSearch); | |
// replace with hard query (todo: pagination) | |
$keywords = DB::get_conn()->escapeString($keywords); | |
$htmlEntityKeywords = htmlentities($keywords, ENT_NOQUOTES, 'UTF-8'); | |
$current_locale = FluentState::singleton()->getLocale(); | |
$sitetree_table_localised = 'SiteTree_Localised_' . $current_locale; | |
$sitetreetables = 'SiteTree_Live.Title, SiteTree_Live.MenuTitle, SiteTree_Live.Content, SiteTree_Live.MetaDescription'; | |
$customResults = DB::query('SELECT DISTINCT SiteTree_Live.ID, SiteTree_Live.Title, | |
MATCH ('.$sitetreetables.') AGAINST (\'' . str_replace(array('*', '+', '-'), '', $keywords) . '\') AS Relevance | |
FROM SiteTree_Live | |
LEFT JOIN SiteTree_Localised AS ' . $sitetree_table_localised . ' | |
ON SiteTree_Live.ID = ' . $sitetree_table_localised . '.RecordID AND ' . $sitetree_table_localised . '.Locale = \'' . $current_locale . '\' AND ((' . $sitetree_table_localised . '.ID IS NOT NULL)) | |
WHERE ( | |
MATCH ('.$sitetreetables.') AGAINST (\'' . $keywords . '\' IN BOOLEAN MODE) | |
+ MATCH ('.$sitetreetables.') AGAINST (\'' . $htmlEntityKeywords . '\' IN BOOLEAN MODE) | |
AND SiteTree_Live.ShowInSearch <> 0 | |
) | |
ORDER BY Relevance DESC | |
LIMIT 20;'); | |
$results = new ArrayList(); | |
foreach ($customResults as $record) { | |
$st = SiteTree::get_by_id($record['ID']); | |
if ( ! empty($st) && $st->canView()) { | |
$results->push($st); | |
} | |
} | |
return $results; | |
} | |
} |
Hmm... that's odd.
Seems not quite related to the fix mentioned above.
Maybe paste the code building this search-form, so more context is given on your specific problem. Or maybe try the https://forum.silverstripe.org/, more people can look at it there...
It's the default that comes with silverstripe. Namely: SilverStripe\CMS\Search\SearchForm.php
can i add dataobjects to the search too?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this... However, when testing this with SS4.7.1 and fluent 5.0.0, my searchbar has a few odd characters. (screenshot). Is there a way to fix this?