Show item data in Searchresults of CMSMS

Postdate: March 22 2012
Category: External Integration
Applies to: all

Well we all know that the standard search module of CMSMS is a little spartanic in its result. Here is a way of improving on that and show an excerpt of your item.


There are 3 easy steps:

  1. We need a Smartyplugin(a modifier) that creates the modulecall for the resulttemplate 
  2. then we need a template to show the results
  3. and the modification to the resulttemplate of SEARCH

1. The Smarty-Plugin

Download it, extract the modifier and save the file to /lib/smarty/plugins/
Here is the Plugin »

The relevant code:

function smarty_modifier_searchresult($string, $mymodule)
{
  preg_match('/(what=)([0-9-a-zA-Z]+).*(alias=)([0-9-a-zA-Z]+)/i',$string, $split_array);

  $output .= "{cms_module module=\"$mymodule\" what='" . $split_array[2] . "' alias='" . $split_array[4] . "' template='search'}";

  return $output;
}

2. The template that shows the excerpt

 Create a detailtemplate for your module with any properties you want and name it search.

Example:

{assign var="item" value=$itemlist[0]}
<a href="{$item->url}">{$item->name}</a>
<p>{$item->your_property|strip_tags|truncate:100}</p>

your_property should be a variable/property of your module.
It is stripped of all HTML tags and truncated to 100 characters to limit the length. Change it to a value of your liking!

3. Calling the searchresults in SEARCH

...
{foreach from=$results item=entry}
  {if $entry->module eq "YOUR_MODULENAME"}
    {eval var=$entry->url|searchresult:$entry->module}
  {else}
  ... 

Now, what does this do?

The modifier searchresult analyzes $entry->url and extracts the what and alias parameter. Then it creates a modulecall with those parameters and the moduleoutput is shown with your newly created search resulttemplate.

That's all. It works and I do it all the time.

If your are good in PHP and can improve the preg_match pattern your are welcome to do so!