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.

Archive /

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

Moderators: martimiz, Sean, biapar, Willr, Ingo

TreeDropdownField in Widget


Go to End
Reply


2 Posts   2242 Views

Avatar
Anatol

126 Posts

10 July 2008 at 2:57pm

Hi all,

I am trying to create a Widget that displays the content of a random sub page, i.e. in the widget I choose a parent page from the site tree, the frontend would then display the content of a random child page of that parent.

I started as you can see below. The TreeDropdownField gets displayed, but when I try to select it I get the error message "Error Getting Data".

RandomContentWidget.php

<?php
 
class RandomContentWidget extends Widget {
	static $has_one = array(
		"RandomContent" => "Page"
	);
 
	static $title = "Random Content";
	static $cmsTitle = "Random Content Widget";
	static $description = "Adds content from a random sub page to the widget sidebar.";
 
	// the random selection is still missing at this stage ...

	function getCMSFields() {
		return new FieldSet(
			new TreeDropdownField("Page", "Choose a random parent page:", "SiteTree")
		);
	}
}
 
?>

RandomContentWidget.ss

<div id="RandomContentWidget">
<% control RandomContent %>
<h3>$Title</h3>
$Content
< end_control %>
</div>

I am not very experienced with creating Silverstripe widgets. I just wrote my very first one yesterday. If anyone knows why I get the "Error Getting Data" message I would be happy for a hint.

Cheers!
Anatol

Avatar
Anatol

126 Posts

10 July 2008 at 6:00pm

It seems the TreeDropDownField does not work properly in widgets yet: http://open.silverstripe.com/ticket/2162.

I borrowed some code from simon_w who seemed to have similar problems with the Children Pages Widget. Thank you for that!

This works:

RandomContentWidget.php

<?php
 
class RandomContentWidget extends Widget {
	static $title = "Random Content";
	static $cmsTitle = "Random Content Widget";
	static $description = "Adds content from a random sub page to the widget sidebar.";
	
	static $db = array(
		"Page"=>"Varchar(255)"
		);
	static $defaults = array(
		"Page"=>"home"
		);
 
	function getCMSFields() {
		// TODO: Use TreeDropdownField when it's fixed
		return new FieldSet(
			new TextField("Page", _t('RandomContentWidget.PAGE', "The URLSegment of the parent page to display a random child of."))
		);
	}
	function RandomContent() {
		$parent = DataObject::get_one("SiteTree", "URLSegment = '". Convert::raw2sql($this->Page) . "'");
		if(!$parent) {
			return false;
		}
		return DataObject::get("SiteTree", "ParentID = $parent->ID", "RAND()", "", 1);
	}
}
 
?>

RandomContentWidget.ss

<div id="RandomContentWidget">
	<% control RandomContent %>
		$Content
	<% end_control %>
</div>

I can upload the two widgets I created once they are cleaned up (the random content widget above and a widget to add html content with the TinyMCE editor).

Cheers!
Anatol