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.

All other Modules /

Discuss all other Modules here.

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

CustomSearchForm snippet


Go to End


46 Posts   17730 Views

Avatar
CraftsMan

Community Member, 35 Posts

28 June 2012 at 2:31am

I am trying to get this working with 2.4. I have a clean install where I am testing this locally but getting a blank results page ( the url is http://localhost/home/SearchForm?Search=Search&action_results=Search )

I cant see anything wrong with my code. I do have a results template. Any idea what might be causing this?

Avatar
CraftsMan

Community Member, 35 Posts

2 July 2012 at 9:59pm

I have got this working since. My problem was the results layout file I was using.

I now have a challenge to show search results based on a linked DO. So I want to list search results form the parent DO based on matches from the child DO. Anyone done anything similar or aware of this can be achieved?

Avatar
Leon223

Community Member, 10 Posts

5 December 2012 at 1:30am

I have optimized this script for Silverstripe 3. If anyone needs it just leave a message and i will upload the script.

Avatar
arnhoe

Community Member, 6 Posts

5 December 2012 at 3:05am

Hello Leon223,

I am interested in your SS3.0 version of this script.

Avatar
BartM

Community Member, 9 Posts

4 February 2013 at 10:58pm

Hello there,

I too am interested in the optimized script. Any chance it can still be uploaded?

Thanks in advance.

Avatar
Tony Air

Community Member, 13 Posts

5 February 2013 at 6:32am

Edited: 05/02/2013 6:42am

Here's the other SS3 snippet

Page_Controller

public function SearchForm(){
		$searchText = _t('SearchForm.SEARCH', 'Search this site ...');

		if($this->request && $this->request->getVar('Search')) {
			$searchText = $this->request->getVar('Search');
		}
		
		$fields = new FieldList(
			$textField = TextField::create('Search',$searchText)
		);		
		$validator = new RequiredFields('Search');
		$actions = new FieldList(FormAction::create('doSearch','Go'));
		$form = new ZZSearchForm($this,'SearchForm',$fields,$actions,$validator);
		$form->disableSecurityToken()->setFormMethod('get');
		
		return $form;
	}
public $isSearched = false;
	public static $classesToSearch = array('SiteTree');
	public function doSearch($data, $form, $request) {
		$this->isSearched = true;
		$limit = 10;
		$form->classesToSearch(self::$classesToSearch)->setPageLength($limit);
	
		$tplData = array(
			'PageResults' => $form->getResultsByClass('SiteTree'),
			'Query' => $form->getSearchQuery(),
			'Title' => _t('SearchForm.SearchResults', 'Search Results')
		);
		
		$ar = $this->extend('updateDoSearch',$tplData,$form);
		if(!empty($ar)){$tplData = $ar[0];} // dirty
		return $this->owner->customise($tplData)->renderWith(array('Page_results', 'Page'));
	}
public function isSearched(){
		return $this->isSearched;
	}

project / _config.php

Page_Controller::$classesToSearch = array('SiteTree','CatItem'); // set classes to search

CatItem

public static $searchable_fields = array('Title','Model','Manufacturer'); // searchable fields

Page_ControllerExtender

public function updateDoSearch($tplData,$form){
		$tplData['CatItemResults'] = $form->getResultsByClass('CatItem')->setPageLength(9); // here u can set any additional data lists for ur templates
		return $tplData;
	}

Then u can parse ur variables in template any way u need, here's my template for an instance:

<div id="PageContainer" class="typography">
	<div id="HeaderContainer">
		<h1>$Title</h1>
	</div>	
	<% if Query %>
		<div class="h2">
			<%t Page.RESULTSQUERY "You searched for &quot;{Query}&quot;" Query=$Query %>
		</div>
	<% end_if %>
	<div class="h3">We have found following Pages</div>
	<% include PageSearch %>	
	<div class="h3">We have found following Items</div>
	<% if CatItemResults %>
	<div id="BasketItems" class="row">
		<ul class="basketItems clearfix">
			<% loop CatItemResults %>
				<% include CatItemTeaser %>
			<% end_loop %>
		</ul>
		<% with CatItemResults %>
			<% include Pagination %>
		<% end_with %>
	</div>
	<% else %>
		<div class="h3"><%t Page.RESULTSNO "Sorry, your search query did not return any results." %></div>
	<% end_if %>
</div>

Pagination.ss

<% if MoreThanOnePage %>
<div class="pagination pagination-centered">
<ul>
	<li class="<% if not NotFirstPage %>disabled <% end_if %>prev">
		<a href="$PrevLink">&laquo;</a>
	</li>
	<% loop Pages %>
		<li<% if CurrentBool %> class="active"<% end_if %>>
			<% if Link %>
				<a href="$Link">$PageNum</a>
        	<% else %>
				<span>...</span>
			<% end_if %>
		</li>
	<% end_loop %>
	<li class="<% if not NotLastPage %>disabled <% end_if %>next">
		<a href="$NextLink">&raquo;</a>
	</li>
</ul>
</div>
<% end_if %>

Go to Top