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.

Data Model Questions /

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

search form results , block image searching


Go to End


5 Posts   2875 Views

Avatar
snaip

Community Member, 181 Posts

19 February 2010 at 10:03am

Hi

I'm using standard search form function

	function SearchForm() {
		$searchText = isset($this->Query) ? $this->Query : '';
		$fields = new FieldSet(
	      	new TextField("Search", "", $searchText)
	  	);
		$actions = new FieldSet(
	      	new FormAction('results', 'find product')
	  	);

	  	return new SearchForm($this, "SearchForm", $fields, $actions);
	}
	
	/**
	 * Process and render search results
	 */
	function results($data, $form){
	
		if($form->getSearchQuery() == NULL) {
			$data = array(
				'Results' => '',
				'Query' => '',
				'Title' => 'Your results'
			);			
		} else {
	
			$data = array(
				'Results' => $form->getResults(),
				'Query' => $form->getSearchQuery(),
				'Title' => 'Your results'
			);
		}

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

but it also searchs image files
if file has a name for example:

kitchen_image_2.jpg

and i want to search "kitchen" , one of the results is this image
also if i have a folder "kitchen" with many images, they also are including to search result

so my question is
how to block image searching ?

Avatar
Hamish

Community Member, 712 Posts

22 February 2010 at 8:44am

In your Page Controller you'll have a method called SearchForm(). The last line should read:

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

Change this to:

$form = new SearchForm($this, "SearchForm", $fields, $actions);
$form->classesToSearch(array('SiteTree'));
return $form;

Avatar
snaip

Community Member, 181 Posts

22 February 2010 at 10:47pm

:D

maby you can help with with second problem
also with image in serach results : )

i have product page with
has_one {
'Img' => 'Image'
}

but standard search form only searching in the SiteTree so i cant display product image
how to modify this search form function to display images ?

Avatar
_Vince

Community Member, 165 Posts

3 April 2011 at 8:50am

If you know that one form needs to search Files and Pages and the other searches JUST the pages and it's ALWAYS going to be like that, couldn't you hardcode a parameter in your template for your overloaded SearchForm() function?

Avatar
Polar Design

Community Member, 2 Posts

31 May 2013 at 6:22am

Not sure if this is the best method, but seems to work as well. In sapphire/search/FulltextSearchable.php, around line 40 it says:

static function enable($searchableClasses = array('SiteTree', 'File')) {
$defaultColumns = array(
'SiteTree' => 'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords',
'File' => 'Filename,Title,Content'
);

which is therefore searching both items in SiteTree and items in File. I had users uploading resumes through the site and those were showing in search results! To counter this, I simply deleted the 'File' information from search and now that line looks like:

static function enable($searchableClasses = array('SiteTree')) {
$defaultColumns = array(
'SiteTree' => 'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords'
);

Now when I search on the site the uploaded resumes don't show up in search results. Again, not sure if this is the best or preferred method, but seems to work.