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.

General Questions

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

MetaDescription in search results


Go to End
Reply

10 Posts   2324 Views

Avatar
DeklinKelly

29 July 2010 at 7:42am Community Member, 197 Posts

I want to show the Meta Description in search results. I am using the standard search form on Silverstripe 2.4.

This code does NOT display the $MetaDescription and I think the problem is that the built-in search functionality does NOT return MetaDescription along with other SiteTree objects.

How can I get the $MetaDescription in these results?

Here is my template code:

<h1>Search Results</h1>
<div class="typography">
   <% if Results %>
    <ul id="SearchResults">
    <% control Results %>
    <li>
    <% if MenuTitle %>
    <h3><a class="searchResultHeader" href="$Link">$MenuTitle</a></h3>
    <% else %>
    <h3><a class="searchResultHeader" href="$Link">$Title</a></h3>
    <% end_if %>
          <% if MetaDescription %>
            <p>$MetaDescription</p>
          <% end_if %>
    <a class="readMoreLink" href="$Link" title="{$Title}">Click Here</a>
    </li>
    <% end_control %>
    </ul>
    <% else %>
    <p>Sorry, your search query did not return any results.</p>
    <% end_if %>

    <% if Results.MoreThanOnePage %>
    <div id="PageNumbers">
    <% if Results.NotLastPage %>
    <a class="next" href="$Results.NextLink" title="View the next page">Next</a>
    <% end_if %>
    <% if Results.NotFirstPage %>
    <a class="prev" href="$Results.PrevLink" title="View the previous page">Prev</a>
    <% end_if %>
    <span>
    <% control Results.SummaryPagination(10) %>
    <% if CurrentBool %>
    $PageNum
    <% else %>
    <a href="$Link" title="View page number $PageNum">$PageNum</a>
    <% end_if %>
    <% end_control %>
    </span>

    </div>
    <% end_if %>
</div>

Thanks!

Avatar
ryanwachtl

30 July 2010 at 1:44am (Last edited: 30 July 2010 1:44am), Community Member, 46 Posts

Hi DeklinKelly,

This Topic should help you out: http://www.silverstripe.org/template-questions/show/276854

- Ryan

Avatar
DeklinKelly

30 July 2010 at 4:06am Community Member, 197 Posts

Thanks. But how do I use this code?

What function or class should I put in mysite/Page.php to make this work?

<?php
class CustomSearch extends SearchForm
{
   
   protected $classesToSearch = array("SiteTree");
   
   public function searchEngine($keywords, $pageLength = null, $sortBy = "Relevance DESC", $extraFilter = "", $booleanSearch = false, $alternativeFileFilter = "", $invertedMatch = false) {
      if(!$pageLength) $pageLength = $this->pageLength;
       $keywords = addslashes($keywords);
   
      $extraFilters = array('SiteTree' => '');
       
       if($booleanSearch) $boolean = "IN BOOLEAN MODE";
       if($extraFilter) {
          $extraFilters['SiteTree'] = " AND $extraFilter";
       }
       
       if($this->showInSearchTurnOn)   $extraFilters['SiteTree'] .= " AND showInSearch <> 0";

      $start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
      $limit = $start . ", " . (int) $pageLength;
      
      $notMatch = $invertedMatch ? "NOT " : "";
      if($keywords) {
         $match['SiteTree'] = "MATCH (Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords) AGAINST ('$keywords' $boolean)";
      
         // We make the relevance search by converting a boolean mode search into a normal one
         $relevanceKeywords = str_replace(array('*','+','-'),'',$keywords);
         $relevance['SiteTree'] = "MATCH (Title) AGAINST ('$relevanceKeywords') + MATCH (Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords) AGAINST ('$relevanceKeywords')";
      } else {
         $relevance['SiteTree'] = 1;
         $match['SiteTree'] = "1 = 1";
      }

      // Generate initial queries and base table names
      $baseClasses = array('SiteTree' => '');
      foreach($this->classesToSearch as $class) {
         $queries[$class] = singleton($class)->extendedSQL($notMatch . $match[$class] . $extraFilters[$class], "");
         $baseClasses[$class] = reset($queries[$class]->from);
      }
      
      // Make column selection lists
      $select = array(
         'SiteTree' => array("ClassName","$baseClasses[SiteTree].ID","ParentID","MetaDescription","Title","URLSegment","Content","LastEdited","Created","_utf8'' AS Filename", "_utf8'' AS Name", "$relevance[SiteTree] AS Relevance", "CanViewType"),
      );
      
      // Process queries
      foreach($this->classesToSearch as $class) {
         // There's no need to do all that joining
         $queries[$class]->from = array(str_replace('`','',$baseClasses[$class]) => $baseClasses[$class]);
         $queries[$class]->select = $select[$class];
         $queries[$class]->orderby = null;
      }

      // Combine queries
      $querySQLs = array();
      $totalCount = 0;
      foreach($queries as $query) {
         $querySQLs[] = $query->sql();
         $totalCount += $query->unlimitedRowCount();
      }
      $fullQuery = implode(" UNION ", $querySQLs) . " ORDER BY $sortBy LIMIT $limit";

      // Get records
      $records = DB::query($fullQuery);

      foreach($records as $record)
         $objects[] = new $record['ClassName']($record);

      if(isset($objects)) $doSet = new DataObjectSet($objects);
      else $doSet = new DataObjectSet();
      
      $doSet->setPageLimits($start, $pageLength, $totalCount);

      return $doSet;
   }
}

?>

Avatar
ryanwachtl

30 July 2010 at 4:13am Community Member, 46 Posts

The instructions are listed here: http://www.silverstripe.org/template-questions/show/276854?start=0#post276961

Avatar
DeklinKelly

30 July 2010 at 4:46am Community Member, 197 Posts

Thanks, the directions say to replace a line, but I don't have that line to replace.

Avatar
ryanwachtl

30 July 2010 at 4:54am Community Member, 46 Posts

What version of SS are you using? What does your current Page.php look like?

Avatar
DeklinKelly

30 July 2010 at 5:13am Community Member, 197 Posts

I use SilverStripe version 2.4.

Here is my Page.php

<?php
class Page extends SiteTree {

   public static $db = array(
   );

   public static $has_one = array(
   );

}
class Page_Controller extends ContentController {

   public static $allowed_actions = array (
   );

   public function init() {
      parent::init();

   }
}

Avatar
ryanwachtl

30 July 2010 at 6:07am Community Member, 46 Posts

Sorry for the confusion looks like that Topic doesn't cover 2.4

Here is a quick and dirty way to get your MetaDescription in your template. It will return the Meta Description for a Page if it exists or is not empty, if not it will return false and your template will fall back to the default display.

in Page.php

...
public static $has_one = array(
);

function getSearchMetaDescription() {
   
if ( $data = DataObject::get_by_id("Page", $this->ID) ) {
if ( $data->MetaDescription != "" ) { return $data->MetaDescription; }
else { return false; }
}
else { return false; }

}
...

then update template Page_results.ss:

replace:

$Content.LimitWordCountXML

with:

<% if SearchMetaDescription %>
<p>$SearchMetaDescription</p>
<% else %>
<p>$Content.LimitWordCountXML</p>
<% end_if %>

Go to Top