Jump to:

23375 Posts in 18175 Topics by 2866 members

General Questions

SilverStripe Forums » General Questions » Add custom search fields to search in frontend?

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

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

Page: 1
Go to End
Author Topic: 1281 Views
  • mco
    Avatar
    Community Member
    14 Posts

    Add custom search fields to search in frontend? Link to this post

    Hi,

    maybe this has been asked a lot of times but I don't get it running...

    I want to add some additional fields of my SiteTree object to be available for fulltext search (the one in the front-end)

    So in my Page.php if have

    class Page extends SiteTree {
    static $db = array(
    'Teaser' => 'Text',
    'TinyURL' => 'Varchar(255)'
       );

       static $searchable_fields = array (
          'Teaser' => array(
             'field' => 'TextField',
             'filter' => 'PartialMatchFilter',
             'title' => 'Teaser Text'
          ),
       );
    ....
    }

    No i entered some text in the Teaser field in the backend and expected the page to be found using the frontend-search.
    AR: wasn't found...

    Did i miss something?
    I use the default search implementation in the Page_Controler:

    public 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);
       }

       public function results($data, $form) {
          $form->classesToSearch(array(
             'SiteTree',
             //'File',
             ));

          $results = $form->getResults();
          $results->removeDuplicates('ID');
           $data = array(
    'Result' => $results,
    'Query' => $form->getSearchQuery(),
           'Title' => 'Search Results'
    );

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

    1281 Views
Page: 1
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.