Remember me? forgot password | register

SilverStripe Forum

10688 Posts in 2795 Topics by 1518 members

Jump to:

Site Builders

If this is your first visit, you will need to register before you can post. However, you can browse all messages below.

Community » SilverStripe Forum » Site Builders » Search Widget - Cannot Access More Information than $Title and $Link
Page: 1 go to end Reply
Author Topic: Search Widget - Cannot Access More Information than $Title and $Link 425 views
  • Garrett
    Garrett's avatar
    Community Member
    63 posts

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

    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

    Last edited: 29 August 2008 at 6:07am

  • Garrett
    Garrett's avatar
    Community Member
    63 posts

    Re: Search Results - Get Data from Other Tables Using ID Link to this post

    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

  • willr
    avatar
    Core Development Team
    1263 posts
    3 SilverStripe Sites 2 SilverStripe Themes

    Re: Search Widget - Cannot Access More Information than $Title and $Link Link to this post

    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 %>.....

  • Garrett
    Garrett's avatar
    Community Member
    63 posts

    Re: Search Widget - Cannot Access More Information than $Title and $Link Link to this post

    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

  • Garrett
    Garrett's avatar
    Community Member
    63 posts

    Re: Search Widget - Cannot Access More Information than $Title and $Link Link to this post

    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;
       }

  • Garrett
    Garrett's avatar
    Community Member
    63 posts

    Re: Search Widget - Cannot Access More Information than $Title and $Link Link to this post

    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

  • willr
    avatar
    Core Development Team
    1263 posts
    3 SilverStripe Sites 2 SilverStripe Themes

    Re: Search Widget - Cannot Access More Information than $Title and $Link Link to this post

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

    425 views
go to top Reply

Currently Online:

Harl

Welcome to our latest member: lorem