Jump to:

10928 Posts in 2612 Topics by 1809 members

All other Modules

SilverStripe Forums » All other Modules » CustomSearchForm snippet

Discuss all other Modules here.

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

Page: 1 2 3 4 5 6
Go to End
Author Topic: 9682 Views
  • CraftsMan
    Avatar
    Community Member
    35 Posts

    Re: CustomSearchForm snippet Link to this post

    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?

  • CraftsMan
    Avatar
    Community Member
    35 Posts

    Re: CustomSearchForm snippet Link to this post

    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?

  • Leon223
    Avatar
    Community Member
    10 Posts

    Re: CustomSearchForm snippet Link to this post

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

  • arnhoe
    Avatar
    Community Member
    6 Posts

    Re: CustomSearchForm snippet Link to this post

    Hello Leon223,

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

  • BartM
    Avatar
    Community Member
    9 Posts

    Re: CustomSearchForm snippet Link to this post

    Hello there,

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

    Thanks in advance.

  • Tony Air
    Avatar
    Community Member
    13 Posts

    Re: CustomSearchForm snippet Link to this post

    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 %>

    Attached Files
    9682 Views
Page: 1 2 3 4 5 6
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.