Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Search Widget - Cannot Access More Information than $Title and $Link

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

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

Page: 1
Go to End
Author Topic: 1553 Views
  • Garrett
    Avatar
    Community Member
    245 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

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Search Widget - Cannot Access More Information than $Title and $Link 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
    Forum Moderator
    5489 Posts

    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
    Avatar
    Community Member
    245 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
    Avatar
    Community Member
    245 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
    Avatar
    Community Member
    245 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
    Forum Moderator
    5489 Posts

    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'?

    1553 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.