Jump to:

5449 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Search Form, Results, Custom Fields

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

Page: 1 2 3
Go to End
Author Topic: 3852 Views
  • stew
    Avatar
    Community Member
    30 Posts

    Search Form, Results, Custom Fields Link to this post

    Hey guys,

    I'm 90% through building a site using Silverstripe (2.4), it's coming along well but I've hit a problem.

    I need the search form to search inside a few custom fields in "DatabasePage" - Address, Category, Description - and as I've discovered it's not currently possible to search the custom fields.

    I also need to display a few of the custom fields in the results page.

    So just a little bit stuck, does anyone know how to solve these two problems?

    Thanks,

    Stewart

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Search Form, Results, Custom Fields Link to this post

    Are you talking ModelAdmin?

  • stew
    Avatar
    Community Member
    30 Posts

    Re: Search Form, Results, Custom Fields Link to this post

    No, just the normal CMS/Pages bit.

    I have had a look at ModelAdmin and might spend a few hours tomorrow seeing if it's easier to do it that way.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Search Form, Results, Custom Fields Link to this post

    Ok, sorry can't be of much more help then...

  • sonet
    Avatar
    Community Member
    33 Posts
  • stew
    Avatar
    Community Member
    30 Posts

    Re: Search Form, Results, Custom Fields Link to this post

    After speaking with the client they want to keep their current shared hosting plan so Sphinx is out of the window sadly. Thanks for the suggestion though, looked almost perfect.

    I've managed to work out a slightly awkward way of getting search working - use the Meta > Keywords bit to add in "Tags"... however I can't pull out the custom fields of each search result, has anyone managed to do this?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Search Form, Results, Custom Fields Link to this post

    ahhhh I mis understood... might something like this help...

    in Page.php

    /**
    * Process and render search results
    */
    function results($data, $form){
       $data = array(
       'Results' => $form->getResults(),
       'Query' => $form->getSearchQuery(),
       'Title' => 'Search Results'
       );

       /////////////////////////////////////////////////////////////
       // Start Custom

       $arrSearchQuery = explode(' ',$form->getSearchQuery());
       $strSearchQuery = '%'.implode('%',$arrSearchQuery).'%';

       $dos = DataObject::get('YOURDATAOBJECT',"FIELD LIKE '".$strSearchQuery."'");

       //make our our result set
       $dosNewRestults = new DataObjectSet();

       if ($dos)
       {
          $arr = $dos->toArray();
          foreach ($arr as $do)
          {
             $p = new Page();
             $p->Title = $do->SOMEFIELD;
             $p->URLSegment = 'SOMEURL?POSTVAR='.$do->SOMEFIELD;;
             $p->Content = $do->SOMEFIELD;;
             $p->Name = $do->SOMEFIELD;;
             $p->Relevance = 1;
             $p->CanViewType = 'Anyone';

             $dosNewRestults->push($p);
          }
       }

       //strip out the html from the results and append them to the results
       //above - so that the ones with things to buy are first
       if ($data['Results']->Count() > 0)
       {
          $arr = $data['Results']->toArray();
          foreach ($arr as $do)
          {
             $do->Content = strip_tags($do->Content);
             $dosNewRestults->push($do);
          }
       }

       $data['Results'] = $dosNewRestults;
       // End Custom
       /////////////////////////////////////////////////////////////

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

    It ain't the best, but it might help.

    Barry

  • stew
    Avatar
    Community Member
    30 Posts

    Re: Search Form, Results, Custom Fields Link to this post

    Thanks Barry!

    I'll take a look at it this afternoon and see what happens

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