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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Widgets /

Discuss SilverStripe Widgets.

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

Random Content addition


Go to End


2 Posts   3163 Views

Avatar
brenjt

Community Member, 1 Post

8 September 2009 at 4:17pm

Edited: 08/09/2009 4:18pm

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.

Avatar
Elender78

Community Member, 22 Posts

7 December 2009 at 3:01am

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.