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.

General Questions /

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

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

Remove images from search results


Go to End
Reply


15 Posts   2831 Views

Avatar
Stompit

Community Member, 17 Posts

9 June 2010 at 3:12pm

Hi Folks, had a look all over for a solutions but cant find anything,

The client has some rather large images at the start of each page, problem is these show up in the search results.

I am using the standard search code from the Silverstripe tutorial.

Is there something I can put into my page.php so that it ignores images??

THANKS :)

D

Avatar
Willr

Forum Moderator, 5521 Posts

9 June 2010 at 5:26pm

Edited: 10/08/2010 1:10pm

From the tutorial you would have code like return new SearchForm, you can use this (at least in 2.4) and set the classes.

$form = new SearchForm(....
$form->classesToSearch(array('SiteTree'));

return $form;

Avatar
Stompit

Community Member, 17 Posts

6 August 2010 at 12:48am

Thanks Willr! and apologies for the late reply, I do appreciate your help :)

One issue holding up progress is that I'm a tard, I can see your logic but the finer points of implementation of such a solution have me flummoxed. Would it be at all possible for you to elaborate?

This is what I am working with:


	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);
	}
	
	function results($data, $form){
	  	$data = array(
	     	'Results' => $form->getResults(),
	     	'Query' => $form->getSearchQuery(),
	      	'Title' => 'Search Results'
	  	);

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

Avatar
Willr

Forum Moderator, 5521 Posts

6 August 2010 at 8:48am

Well its pretty much exactly like I said. You currently have

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

You need to change this to

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

return $form;

Avatar
Stompit

Community Member, 17 Posts

9 August 2010 at 12:54pm

Edited: 09/08/2010 1:01pm

Thanks Willr, I did exactly as you instructed but I am getting:

Website Error
There has been an error
The website server has not been able to respond to your request.

I am running 2.4

Thanks :)

Code:


	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);
		$form = new SearchForm($this, "SearchForm", $fields, $actions); 
		$form->classesToSearch('SiteTree');

		return $form;

	}
	
	function results($data, $form){
	  	$data = array(
	     	'Results' => $form->getResults(),
	     	'Query' => $form->getSearchQuery(),
	      	'Title' => 'Search Results'
	  	);

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

Avatar
Willr

Forum Moderator, 5521 Posts

9 August 2010 at 5:21pm

Please put the site into dev mode to get the actual error http://doc.silverstripe.org/debugging#dev_mode

Avatar
Stompit

Community Member, 17 Posts

10 August 2010 at 12:37am

Thanks again for your help and patience, here is the expanded error message:

[Warning] array_diff() [function.array-diff]: Argument #1 is not an array
GET /home/SearchForm?Search=security&action_results=Search

Line 74 in /home/content/p/r/i/###/html/sapphire/search/SearchForm.php

Source

65 			'Form'
66 		));
67 	}
68 
69 	/**
70 	 * Set the classes to search.
71 	 * Currently you can only choose from "SiteTree" and "File", but a future version might improve this. 
72  	 */
73 	function classesToSearch($classes) {
74 		$illegalClasses = array_diff($classes, array('SiteTree', 'File'));
75 		if($illegalClasses) {
76 			user_error("SearchForm::classesToSearch() passed illegal classes '" . implode("', '", $illegalClasses) . "'.  At this stage, only File and SiteTree are allowed", E_USER_WARNING);
77 		}
78 		$legalClasses = array_intersect($classes, array('SiteTree', 'File'));		
79 		$this->classesToSearch = $legalClasses;
80 	}
Trace

array_diff(SiteTree,Array) 
Line 74 of SearchForm.php
SearchForm->classesToSearch(SiteTree) 
Line 26 of Page.php
Page_Controller->SearchForm(SS_HTTPRequest) 
Line 193 of Controller.php
Controller->handleAction(SS_HTTPRequest) 
Line 134 of RequestHandler.php
RequestHandler->handleRequest(SS_HTTPRequest) 
Line 147 of Controller.php
Controller->handleRequest(SS_HTTPRequest) 
Line 199 of ContentController.php
ContentController->handleRequest(SS_HTTPRequest) 
Line 67 of ModelAsController.php
ModelAsController->handleRequest(SS_HTTPRequest) 
Line 283 of Director.php
Director::handleRequest(SS_HTTPRequest,Session) 
Line 127 of Director.php
Director::direct(/home/SearchForm) 
Line 127 of main.php

Avatar
Willr

Forum Moderator, 5521 Posts

10 August 2010 at 1:10pm

Ah I had a typo. It should be

$form->classesToSearch(array('SiteTree'));

I've updated the code above to clarify.

Go to Top