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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

How to extend SearchForm in SS v.3


Go to End


6 Posts   6873 Views

Avatar
pga

Community Member, 8 Posts

21 September 2012 at 1:28am

Edited: 21/09/2012 2:06am

I use SilverStripe 3.x. and I need to make some changes in SearchForm class - e.g. modify search results page length.
How can I do it?

The following code in mysite/code/MySearchForm.php file
unfortunately doesn't work :-(

class MySearchForm extends SearchForm {
	protected $pageLength = 100;	
}

Thanks!

Avatar
Willr

Forum Moderator, 5523 Posts

22 September 2012 at 4:49pm

Edited: 22/09/2012 4:50pm

You can override the default search form by copying the form from ContentControllerSearchExtension to your Page class then alter it as you need.

class Page_Controller extends ContentController {
	public function SearchForm() {
		$searchText =  _t('SearchForm.SEARCH', 'Search');

		if($this->owner->request && $this->owner->request->getVar('Search')) {
			$searchText = $this->owner->request->getVar('Search');
		}

		$fields = new FieldList(
			new TextField('Search', false, $searchText)
		);
		$actions = new FieldList(
			new FormAction('results', _t('SearchForm.GO', 'Go'))
		);
		$form = new SearchForm($this->owner, 'SearchForm', $fields, $actions);
		$form->classesToSearch(FulltextSearchable::get_searchable_classes());
		$form->setPageLength(1000);

		return $form;
}
}

Avatar
marc79

Community Member, 65 Posts

10 January 2014 at 12:34am

Hello

I'm trying to extend the search form to include data beyond the default specified in the FulltextSearchable file but am not sure I understand the annotation.

/*
Enable the default configuration of MySQL full-text searching on the given data classes.
It can be used to limit the searched classes, but not to add your own classes.
For this purpose, please use {@link Object::add_extension()} directly:
<code>
MyObject::add_extension("FulltextSearchable('MySearchableField,'MyOtherField')");
</code>

Caution: This is a wrapper method that should only be used in _config.php,
and only be called once in your code.

@param Array $searchableClasses The extension will be applied to all DataObject subclasses
listed here. Default: {@link SiteTree} and {@link File}.
*/

From this I understand that you can't just add them in as below:

public static function enable($searchableClasses = array('SiteTree', 'File', 'GalleryPage_Images')) {
$defaultColumns = array(
'SiteTree' => '"Title","MenuTitle","Content","MetaDescription"',
'File' => '"Filename","Title","Content"',
'GalleryPage_Images' => '"Caption"'
);

Do I need to create a custom version of the file FulltextSearchable and if wo where would this need to be stored? Or do I create a new function within the FulltextSearchable file?

Thanks

Avatar
marc79

Community Member, 65 Posts

24 January 2014 at 11:23pm

I've spent some hours looking at this and am don't think I am getting any closer.

As mentioned in another post if I search for nothing all images are returned. However, as soon as you enter a search term you get nothing back.

In my config file I now have:

FulltextSearchable::enable('File'); <---- Have excluded SiteTree as I am only really interested in returning image.

GalleryImage::add_extension("FulltextSearchable('Name','ImageName','SearchTags')");

I've then created a new DataObject called GalleryImage and added some data which doesn't appear to be picked up at all, even with a blank search.

Any help on this would be much appreciated. Am I heading in the right direction?

Thanks

Avatar
Willr

Forum Moderator, 5523 Posts

25 January 2014 at 8:32am

Extending FulltextSearchable is a bit of a nightmare. I'm picking all search functionality will be stripped out of core pretty soon. You could do something like https://gist.github.com/colymba/4657819

Avatar
marc79

Community Member, 65 Posts

31 January 2014 at 5:48am

Thanks Willr I will take a look at this.