Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Add custom search fields to search in frontend?


Go to End


1846 Views

Avatar
mco

Community Member, 14 Posts

29 September 2010 at 4:11am

Hi,

maybe this has been asked a lot of times but I don't get it running...

I want to add some additional fields of my SiteTree object to be available for fulltext search (the one in the front-end)

So in my Page.php if have

class Page extends SiteTree {
static $db = array(
'Teaser' => 'Text',
'TinyURL' => 'Varchar(255)'
);

static $searchable_fields = array (
'Teaser' => array(
'field' => 'TextField',
'filter' => 'PartialMatchFilter',
'title' => 'Teaser Text'
),
);
....
}

No i entered some text in the Teaser field in the backend and expected the page to be found using the frontend-search.
AR: wasn't found...

Did i miss something?
I use the default search implementation in the Page_Controler:

public function SearchForm() {
$searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Search';
$fields = new FieldSet(
new TextField("Search", "", $searchText)
);
$actions = new FieldSet(
new FormAction('results', 'Search')
);

return new SearchForm($this, "SearchForm", $fields, $actions);
}

public function results($data, $form) {
$form->classesToSearch(array(
'SiteTree',
//'File',
));

$results = $form->getResults();
$results->removeDuplicates('ID');
$data = array(
'Result' => $results,
'Query' => $form->getSearchQuery(),
'Title' => 'Search Results'
);

return $this->customise($data)->renderWith(array('Article_proc', 'Page'));
}