Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » search form results , block image searching

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

Page: 1
Go to End
Author Topic: 1878 Views
  • snaip
    Avatar
    Community Member
    180 Posts

    search form results , block image searching Link to this post

    Hi

    I'm using standard search form function

       function SearchForm() {
          $searchText = isset($this->Query) ? $this->Query : '';
          $fields = new FieldSet(
           new TextField("Search", "", $searchText)
           );
          $actions = new FieldSet(
           new FormAction('results', 'find product')
           );

           return new SearchForm($this, "SearchForm", $fields, $actions);
       }
       
       /**
        * Process and render search results
        */
       function results($data, $form){
       
          if($form->getSearchQuery() == NULL) {
             $data = array(
                'Results' => '',
                'Query' => '',
                'Title' => 'Your results'
             );         
          } else {
       
             $data = array(
                'Results' => $form->getResults(),
                'Query' => $form->getSearchQuery(),
                'Title' => 'Your results'
             );
          }

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

    but it also searchs image files
    if file has a name for example:

    kitchen_image_2.jpg

    and i want to search "kitchen" , one of the results is this image
    also if i have a folder "kitchen" with many images, they also are including to search result

    so my question is
    how to block image searching ?

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: search form results , block image searching Link to this post

    In your Page Controller you'll have a method called SearchForm(). The last line should read:

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

    Change this to:

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

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: search form results , block image searching Link to this post

    maby you can help with with second problem
    also with image in serach results : )

    i have product page with
    has_one {
    'Img' => 'Image'
    }

    but standard search form only searching in the SiteTree so i cant display product image
    how to modify this search form function to display images ?

  • _Vince
    Avatar
    Community Member
    161 Posts

    Re: search form results , block image searching Link to this post

    If you know that one form needs to search Files and Pages and the other searches JUST the pages and it's ALWAYS going to be like that, couldn't you hardcode a parameter in your template for your overloaded SearchForm() function?

  • Polar Design
    Avatar
    Community Member
    2 Posts

    Re: search form results , block image searching Link to this post

    Not sure if this is the best method, but seems to work as well. In sapphire/search/FulltextSearchable.php, around line 40 it says:

       static function enable($searchableClasses = array('SiteTree', 'File')) {
          $defaultColumns = array(
             'SiteTree' => 'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords',
             'File' => 'Filename,Title,Content'
          );

    which is therefore searching both items in SiteTree and items in File. I had users uploading resumes through the site and those were showing in search results! To counter this, I simply deleted the 'File' information from search and now that line looks like:

       static function enable($searchableClasses = array('SiteTree')) {
          $defaultColumns = array(
             'SiteTree' => 'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords'
          );

    Now when I search on the site the uploaded resumes don't show up in search results. Again, not sure if this is the best or preferred method, but seems to work.

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