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.

Archive /

Our old forums are still available as a read-only archive.

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

Search Widget - Cannot Access More Information than $Title and $Link


Reply


7 Posts   1637 Views

Avatar
Garrett

Community Member, 245 Posts

29 August 2008 at 6:07am

Edited: 29/08/2008 6:07am

Hi,

I am trying to use the built-in Silverstripe Search widget to search my Blog only. So I have turned off the "Show in Search" checkbox on all objects in my CMS except for Blog Entries, and that works fine.

However, I cannot refer to variables which are specific to Blog Entries when displaying my Search Results. I can only access $Title and $Link. I would also like to include other information such as $ParagraphSummary, $Author, $Date, etc. etc. much like on the Blog landing page.

How can I achieve this? Is this because my SearchForm() is defined in Page.php instead of BlogHolder.php?

Any help offered would be greatly appreciated.

Thanks,
Garrett

Avatar
Garrett

Community Member, 245 Posts

30 August 2008 at 7:20am

Hello again,

I have not received any response to my previous post so I thought I'd be more specific as to my issue. In Page_results.ss, the template used to display the search results, I would like to be able to display more information on the pages found than just the title.

I want to pass the SiteTree ID from inside the <% control Results %> loop to a function which takes that ID and gets the other fields from the BlogEntry table:

function BlogPostInfo($identifier) {
      
      $results = new DataObjectSet();
      
      $blog_posts = DB::query("SELECT * FROM BlogEntry, SiteTree WHERE BlogEntry.ID = SiteTree.ID AND BlogEntry.ID = '". $identifier ."'");
      
      foreach($blog_posts as $sqlResult) {

         $Author = $sqlResult['Author'];
         $Date = $sqlResult['Date'];
         $Tags = $sqlResult['Tags'];
      
         $results->push(new ArrayData(array(
            "Author" => $Author,
            "Date" => $Date,
            "Tags" => $Tags
         )));
         
      }
      
      return $results;
   }

So, inside my <% control Results %> loop, I have this:

<% control BlogPostInfo(ID) %>

Aaaaaand nothing happens! What am I doing wrong here? How can this sort of thing be achieved? Surely I should be able to get the other fields from a Page in the Results list, no?

Thanks again,
Garrett

Avatar
Willr

Forum Moderator, 5513 Posts

30 August 2008 at 11:13am

SSViewer cannot handle passing Variables such as $ID, $Title to a function. It just doesnt work :(. What you have to do is find the Results() method on your search and in that (I dont know what it looks like) but you would need to do something like this (add this at the end of the method, before it gets returned)

function Results()
...
..
// before you return the list of results attach the blog info
foreach($results as $result) {
$result->BlogInfo = $this->BlogPostInfo($result->ID);
}
return $results.

Then in the template you just need to do <% if BlogInfo %><% control BlogInfo %>.....

Avatar
Garrett

Community Member, 245 Posts

3 September 2008 at 2:38am

Can't thank you enough for your help, willr. Tremendous. This makes good sense. However, I am confused by your use of "BlogInfo", as this appears nowhere in my code. My function is called BlogPostInfo(). Is this some other variable you are inventing? Also, where should the BlogPostInfo() function actually LIVE? The same place that GenericDataAdmin.php (that's where Results() is) or in Page.php, or...?

Thanks again,
Garrett

Avatar
Garrett

Community Member, 245 Posts

3 September 2008 at 3:26am

I have an update to this issue. I am now accessing the BlogInfo variable from within the template; however the $result->ID is returning the ID of the BlogHOLDER instead of the Blog Entries themselves, so the ID's never match up. This is a weird one, as my query works fine in the Query Browser.

Here is the results() function, modified:

function results($data, $form) {

      $data = array(
   'Results' => $form->getResults(),
   'Query' => $form->getSearchQuery(),
   'Title' => 'Search Results'
   );
      
      $results = $this->customise($data);
      
      foreach($results as $result) {
         $result->BlogInfo = $this->BlogPostInfo($result->ID);
      }
      
      $results = $results->renderWith(array('Page_results', 'Page'));
      
      return $results;
   }

Avatar
Garrett

Community Member, 245 Posts

4 September 2008 at 8:39am

Hi,

I got this all sorted out and am getting the fieelds I need from the BlogEntry table. Thanks a lot for your help.

One more thing, though-- how come I can't perform a Long() on the Date?? When I say, "$Date.Long" or ".Nice" in the template, the Date disappears.

//Garrett

Avatar
Willr

Forum Moderator, 5513 Posts

4 September 2008 at 10:40am

Hmm thats strange.. is the $Date a field of type 'Date'?