Jump to:

794 Posts in 408 Topics by 304 members

Widgets

SilverStripe Forums » Widgets » RandomContentWidget

Discuss SilverStripe Widgets.

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

Page: 1
Go to End
Author Topic: 1219 Views
  • njw
    Avatar
    Community Member
    7 Posts

    RandomContentWidget Link to this post

    I am not sure whether this belongs here or in the Themes forum, so apologies if it is in the wrong place.

    I am new to Silverstripe and I am using an amended version of the Dark Ritual theme (only the CSS is amended to date). I would like to put the RandomContentWidget onto the sidebar, but when I go to the Home page, the enabled widget sidebar does not show.

    Is there a way to code the widget in to the page.ss template, replacing the hardcoded "Lobontis" text? And if so, how please!

    Many thanks for any help.

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: RandomContentWidget Link to this post

    As per the other topic:

    It can be done as far as I know, instructions here:
    http://doc.silverstripe.com/doku.php?id=widgets#adding_widgets_to_other_pages

    Cheers,

    Rich

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: RandomContentWidget Link to this post

    I do this manually for displaying random product pages on the homepage. Quite easy:

    In HomePage.php inside HomePage_Controller class:

       // Get featured tour data
       function getFeaturedTour() {
          return DataObject::get_one('Tour', 'TourLengthDays>0', false, 'RAND()');
       }

    Where 'Tour' is the page type and the second argument is a WHERE statement.

    Then in the HomePage.ss template:

    <% control getFeaturedTour %>
    <div class="featured">
    <div class="featured-top">
    <div class="featured-btm">
    <h4>Featured Tour</h4>
    <a href="$Link">
    $Photo2.SetWidth(205)
    </a>
    <h5 style="margin: 0px">
    <a href="$Link" style="color:#023107">$Title</a>
    <small>$TourLeavesFrom to $TourEndsAt - $TourLengthDays Days</small>
    </h5>
    <% if TourTeaser %>
    <div style="font-weight: bold; text-align: justify; margin-right: 5px"><b>$TourTeaser</b></div>
    <p><a href="$Link">Click for details</a></p>
    <% end_if %>
    </div>
    </div>
    </div>
    <% end_control %>

    As you can see I have access to all fields in that Page Type now.

    Cheers
    Aaron

  • njw
    Avatar
    Community Member
    7 Posts

    Re: RandomContentWidget Link to this post

    Thanks Aaron. That looks to solve my problem - and perhaps a few more!

    One question: your code references a field Page Type. I am using standard pages for the text I want to display. I've looked at the database, on the SiteTree table and can't find that field. The closest would appear to be ClassName, but that has a fixed list. What am I missing?

    Many thanks

    Neil

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: RandomContentWidget Link to this post

    Yes, "Tour" is a custom page type that I made. PageType is not a data element.

    If you are just using standard pages for your content, just replace "Tour" with "Page" in my code, and remove the "TourLengthDays>0" select query.

    Cheers
    Aaron

  • njw
    Avatar
    Community Member
    7 Posts

    Re: RandomContentWidget Link to this post

    Thanks again Aaron. In case anyone finds it useful, this is how I amended Aaron's code.

    In Page.php

    // Get featured testimonial
    function getFeaturedTestimonial() {
    return DataObject::get_one('Page', 'ParentID=13', false, 'RAND()');
    }


    I have all the pages I want to display under the same Parent Page. The page ID is 13 - this works, but it would be nice to have it work off the page title instead.

    In Page.ss

    <% control getFeaturedTestimonial %>
       <h4>What Our Clients Think</h4>
       <blockquote>
          <h4 style="margin: 0px">$Title</h4>
          $Content
       </blockquote>
    <% end_control %>

    Thanks again Aaron

    Neil

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: RandomContentWidget Link to this post

    but it would be nice to have it work off the page title instead

    An even more organised way of doing this would be to go back to what I said about Page Types.

    I assume you only want the code to work of the parent title because you want it to say something like "ParentTitle = 'Testimonials'" so there is never any risk of accidently loading content from an incorrect Page.

    You could instead create a custom "TestimonialPage" for these pages, and that would be as easy as this:

    /mysite/code/Testimonial.php

    class Testimonial extends Page {
       static $db = array(
       );
       static $has_one = array(
       );
       
    }

    class Testimonial_Controller extends Page_Controller {

       
    }

    Change the testimonials to be this page type on the behaviors tab (at this stage, there is no difference between this and the original Page).

    Then your random code can be like this:

    // Get featured testimonial
    function getFeaturedTestimonial() {
    return DataObject::get_one('Testimonial', null, false, 'RAND()');
    }

    Easier to follow, you will always only get Testimonials returned by the code, and you also get the option of adding specialized fields and styling to this page type should you wish.

    Just alot more organised.

    Cheers
    Aaron

  • njw
    Avatar
    Community Member
    7 Posts

    Re: RandomContentWidget Link to this post

    Yep, that was what I was looking for!

    Many thanks

    Neil

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