Skip to content

Commit

Permalink
Merge pull request #262 from creative-commoners/pulls/2.1/cms-search-…
Browse files Browse the repository at this point in the history
…theme

FIX CMS search now takes frontend themes into account instead of crashing
  • Loading branch information
NightJar authored Jul 17, 2018
2 parents 28c52d3 + e65fac5 commit 540a757
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Controllers/ElementSiteTreeFilterSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use DNADesign\Elemental\Extensions\ElementalPageExtension;
use SilverStripe\CMS\Controllers\CMSSiteTreeFilter_Search;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\DateField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataList;
use SilverStripe\View\SSViewer;

class ElementSiteTreeFilterSearch extends CMSSiteTreeFilter_Search
{
Expand All @@ -30,6 +32,10 @@ protected function applyDefaultFilters($query)
return parent::applyDefaultFilters($query);
}

// Enable frontend themes in order to correctly render the elements as they would be for the frontend
Config::nest();
SSViewer::set_themes(SSViewer::config()->get('themes'));

// Get an array of SiteTree record IDs that match the search term in nested element data
/** @var ArrayList $siteTrees */
$siteTrees = $query->filterByCallback(function (SiteTree $siteTree) {
Expand All @@ -43,6 +49,9 @@ protected function applyDefaultFilters($query)
return (bool) stripos($pageContent, $this->params['Term']) !== false;
});

// Return themes back for the CMS
Config::unnest();

if ($siteTrees->count()) {
// Apply the list of IDs as an extra filter
$this->extraTermFilters['ID:ExactMatch'] = $siteTrees->column('ID');
Expand Down

0 comments on commit 540a757

Please sign in to comment.