Jump to:

22979 Posts in 11815 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Remove images from search results

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 2
Go to End
Author Topic: 2093 Views
  • Stompit
    Avatar
    Community Member
    17 Posts

    Remove images from search results Link to this post

    Hi Folks, had a look all over for a solutions but cant find anything,

    The client has some rather large images at the start of each page, problem is these show up in the search results.

    I am using the standard search code from the Silverstripe tutorial.

    Is there something I can put into my page.php so that it ignores images??

    THANKS

    D

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Remove images from search results Link to this post

    From the tutorial you would have code like return new SearchForm, you can use this (at least in 2.4) and set the classes.

    $form = new SearchForm(....
    $form->classesToSearch(array('SiteTree'));

    return $form;

  • Stompit
    Avatar
    Community Member
    17 Posts

    Re: Remove images from search results Link to this post

    Thanks Willr! and apologies for the late reply, I do appreciate your help

    One issue holding up progress is that I'm a tard, I can see your logic but the finer points of implementation of such a solution have me flummoxed. Would it be at all possible for you to elaborate?

    This is what I am working with:

       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);
       }
       
       function results($data, $form){
           $data = array(
           'Results' => $form->getResults(),
           'Query' => $form->getSearchQuery(),
           'Title' => 'Search Results'
           );

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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Remove images from search results Link to this post

    Well its pretty much exactly like I said. You currently have

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

    You need to change this to

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

    return $form;

  • Stompit
    Avatar
    Community Member
    17 Posts

    Re: Remove images from search results Link to this post

    Thanks Willr, I did exactly as you instructed but I am getting:

    Website Error
    There has been an error
    The website server has not been able to respond to your request.

    I am running 2.4

    Thanks

    Code:

       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);
          $form = new SearchForm($this, "SearchForm", $fields, $actions);
          $form->classesToSearch('SiteTree');

          return $form;

       }
       
       function results($data, $form){
           $data = array(
           'Results' => $form->getResults(),
           'Query' => $form->getSearchQuery(),
           'Title' => 'Search Results'
           );

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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Remove images from search results Link to this post

    Please put the site into dev mode to get the actual error http://doc.silverstripe.org/debugging#dev_mode

  • Stompit
    Avatar
    Community Member
    17 Posts

    Re: Remove images from search results Link to this post

    Thanks again for your help and patience, here is the expanded error message:

    [Warning] array_diff() [function.array-diff]: Argument #1 is not an array
    GET /home/SearchForm?Search=security&action_results=Search

    Line 74 in /home/content/p/r/i/###/html/sapphire/search/SearchForm.php

    Source

    65          'Form'
    66       ));
    67    }
    68
    69    /**
    70     * Set the classes to search.
    71     * Currently you can only choose from "SiteTree" and "File", but a future version might improve this.
    72     */
    73    function classesToSearch($classes) {
    74       $illegalClasses = array_diff($classes, array('SiteTree', 'File'));
    75       if($illegalClasses) {
    76          user_error("SearchForm::classesToSearch() passed illegal classes '" . implode("', '", $illegalClasses) . "'. At this stage, only File and SiteTree are allowed", E_USER_WARNING);
    77       }
    78       $legalClasses = array_intersect($classes, array('SiteTree', 'File'));      
    79       $this->classesToSearch = $legalClasses;
    80    }
    Trace

    array_diff(SiteTree,Array)
    Line 74 of SearchForm.php
    SearchForm->classesToSearch(SiteTree)
    Line 26 of Page.php
    Page_Controller->SearchForm(SS_HTTPRequest)
    Line 193 of Controller.php
    Controller->handleAction(SS_HTTPRequest)
    Line 134 of RequestHandler.php
    RequestHandler->handleRequest(SS_HTTPRequest)
    Line 147 of Controller.php
    Controller->handleRequest(SS_HTTPRequest)
    Line 199 of ContentController.php
    ContentController->handleRequest(SS_HTTPRequest)
    Line 67 of ModelAsController.php
    ModelAsController->handleRequest(SS_HTTPRequest)
    Line 283 of Director.php
    Director::handleRequest(SS_HTTPRequest,Session)
    Line 127 of Director.php
    Director::direct(/home/SearchForm)
    Line 127 of main.php

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Remove images from search results Link to this post

    Ah I had a typo. It should be

    $form->classesToSearch(array('SiteTree'));

    I've updated the code above to clarify.

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