Jump to:

791 Posts in 369 Topics by 303 members

Widgets

SilverStripe Forums » Widgets » Random Content addition

Discuss SilverStripe Widgets.

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

Page: 1
Go to End
Author Topic: 2008 Views
  • brenjt
    Avatar
    Community Member
    1 Post

    Random Content addition Link to this post

    Just in case anyone was wondering. Here is a simple way that you can have random content show up, from more than one parent page, with the Random Content Widget.

    In your RandomContentWidget.php, add more parent pages to the array, naming them whatever you want.

    static $db = array(
          'WidgetTitle' => 'Varchar(255)',
          'ParentPage'=>'Varchar(255)',
          'ParentPage2'=>'Varchar(255)',
          'ParentPage3'=>'Varchar(255)',
          'ParentPage4'=>'Varchar(255)',
          'ParentPage5'=>'Varchar(255)'
          );

    Then add more fields to the admin widget

    function getCMSFields() {
          // TODO: Use TreeDropdownField when it's fixed
          return new FieldSet(
             new TextField('WidgetTitle', _t('TITLE','Title (optional)')),
             new TextField('ParentPage', _t('PARENTPAGE', 'The URL segment of the parent page to display a random child of.')),
             new TextField('ParentPage2', _t('PARENTPAGE2', 'The URL segment of the second parent page.')),
             new TextField('ParentPage3', _t('PARENTPAGE3', 'The URL segment of the third parent page')),
             new TextField('ParentPage4', _t('PARENTPAGE4', 'The URL segment of the fourth parent page')),
             new TextField('ParentPage5', _t('PARENTPAGE5', 'The URL segment of the fifth parent page'))
          );
       }

    With that we need to change the RandomContent function to randomly pull a parent page that has been defined. We will create an array with all the parent pages in them. As seen below. Then in the Array_rand function pull the total number of parent pages that you have. Which in this example there are five. Last but not least change "Convert::raw2sql($this->ParentPage)" to "Convert::raw2sql($randomParentPage)"

    function RandomContent() {
          
          $allParents = array($this->ParentPage, $this->ParentPage2, $this->ParentPage3, $this->ParentPage4, $this->ParentPage5);
          $rand_keys = array_rand($allParents, 5);
          $randomParentPage = $allParents[$rand_keys[0]];
          $parent = DataObject::get_one('SiteTree', "URLSegment = '". Convert::raw2sql($randomParentPage) . "'");
          if(!$parent) {
             return false;
          }
          return DataObject::get('SiteTree', "ParentID = $parent->ID", "RAND()", "", 1);
       }

    And that should do it. Then in your RandomContentWidget.ss call which ever info you would like to be shown. For my website i have an image that is being pulled with the date created for each random page like so.

    <% control RandomContent %>
    <a href="$Link"><img src="$Image.URL" width="206" /></a>
    CREATED: $Date
    <% end_control %>

    I hope you find this useful and if anyone can expand on it, please share.

  • Elender78
    Avatar
    Community Member
    22 Posts

    Re: Random Content addition Link to this post

    Hey brenjt,

    thank you very much for this extension of the randomContentWidget. I changed the number of displayed items to 3. It worked very well on my page for one parent-url. but yesterday i added 3 other parent-urls, and it isn't working at all now. The only thing it displays is the Headline of the whole widget. Do you know what error here occurs?

    Greetings,

    M.

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