Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » How to extend SearchForm in SS v.3

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

Page: 1
Go to End
Author Topic: 1214 Views
  • pga
    Avatar
    Community Member
    8 Posts

    How to extend SearchForm in SS v.3 Link to this post

    I use SilverStripe 3.x. and I need to make some changes in SearchForm class - e.g. modify search results page length.
    How can I do it?

    The following code in mysite/code/MySearchForm.php file
    unfortunately doesn't work

    class MySearchForm extends SearchForm {
       protected $pageLength = 100;   
    }

    Thanks!

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: How to extend SearchForm in SS v.3 Link to this post

    You can override the default search form by copying the form from ContentControllerSearchExtension to your Page class then alter it as you need.

    class Page_Controller extends ContentController {
       public function SearchForm() {
          $searchText = _t('SearchForm.SEARCH', 'Search');

          if($this->owner->request && $this->owner->request->getVar('Search')) {
             $searchText = $this->owner->request->getVar('Search');
          }

          $fields = new FieldList(
             new TextField('Search', false, $searchText)
          );
          $actions = new FieldList(
             new FormAction('results', _t('SearchForm.GO', 'Go'))
          );
          $form = new SearchForm($this->owner, 'SearchForm', $fields, $actions);
          $form->classesToSearch(FulltextSearchable::get_searchable_classes());
          $form->setPageLength(1000);

          return $form;
    }
    }

  • marc79
    Avatar
    Community Member
    65 Posts

    Re: How to extend SearchForm in SS v.3 Link to this post

    Hello

    I'm trying to extend the search form to include data beyond the default specified in the FulltextSearchable file but am not sure I understand the annotation.

    /*
    Enable the default configuration of MySQL full-text searching on the given data classes.
    It can be used to limit the searched classes, but not to add your own classes.
    For this purpose, please use {@link Object::add_extension()} directly:
    <code>
    MyObject::add_extension("FulltextSearchable('MySearchableField,'MyOtherField')");
    </code>

    Caution: This is a wrapper method that should only be used in _config.php,
    and only be called once in your code.

    @param Array $searchableClasses The extension will be applied to all DataObject subclasses
    listed here. Default: {@link SiteTree} and {@link File}.
    */

    From this I understand that you can't just add them in as below:

    public static function enable($searchableClasses = array('SiteTree', 'File', 'GalleryPage_Images')) {
          $defaultColumns = array(
             'SiteTree' => '"Title","MenuTitle","Content","MetaDescription"',
             'File' => '"Filename","Title","Content"',
             'GalleryPage_Images' => '"Caption"'
          );

    Do I need to create a custom version of the file FulltextSearchable and if wo where would this need to be stored? Or do I create a new function within the FulltextSearchable file?

    Thanks

  • marc79
    Avatar
    Community Member
    65 Posts

    Re: How to extend SearchForm in SS v.3 Link to this post

    I've spent some hours looking at this and am don't think I am getting any closer.

    As mentioned in another post if I search for nothing all images are returned. However, as soon as you enter a search term you get nothing back.

    In my config file I now have:

    FulltextSearchable::enable('File'); <---- Have excluded SiteTree as I am only really interested in returning image.

    GalleryImage::add_extension("FulltextSearchable('Name','ImageName','SearchTags')");

    I've then created a new DataObject called GalleryImage and added some data which doesn't appear to be picked up at all, even with a blank search.

    Any help on this would be much appreciated. Am I heading in the right direction?

    Thanks

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: How to extend SearchForm in SS v.3 Link to this post

    Extending FulltextSearchable is a bit of a nightmare. I'm picking all search functionality will be stripped out of core pretty soon. You could do something like https://gist.github.com/colymba/4657819

  • marc79
    Avatar
    Community Member
    65 Posts

    Re: How to extend SearchForm in SS v.3 Link to this post

    Thanks Willr I will take a look at this.

    1214 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.