Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

Search Form, Results, Custom Fields


Go to End
Reply

17 Posts   4224 Views

Avatar
stew

15 July 2010 at 2:32am Community Member, 30 Posts

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

Avatar
swaiba

15 July 2010 at 3:09am Forum Moderator, 1796 Posts

Are you talking ModelAdmin?

Avatar
stew

15 July 2010 at 3:15am (Last edited: 15 July 2010 3:15am), Community Member, 30 Posts

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.

Avatar
swaiba

15 July 2010 at 3:16am Forum Moderator, 1796 Posts

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

Avatar
sonet

15 July 2010 at 5:03am Community Member, 33 Posts

You can check the http://silverstripe.org/sphinx-module module.

Avatar
stew

15 July 2010 at 10:38pm Community Member, 30 Posts

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?

Avatar
swaiba

15 July 2010 at 10:44pm (Last edited: 15 July 2010 10:44pm), Forum Moderator, 1796 Posts

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

Avatar
stew

15 July 2010 at 10:45pm Community Member, 30 Posts

Thanks Barry!

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

Go to Top