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.

Customising the CMS /

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

Creating a user selection in the backend using TreeDropdownField

Go to End

3 Posts   1302 Views


Community Member, 104 Posts

16 January 2010 at 12:48am


I've got a promotional box on my website and I want to have the ability to select a site from the SiteTree through the backend, where the user will be forwarded to, when clicking on the box (image, text and button). It should be very similar to inserting a link, so I tried to use TreeDropdownField.

My code:

class Box extends Page {
	static $db = array();
	static $has_one = array(
		'Photo' => 'Image',
		'TargetContent' => 'SiteTree'
	function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab("Root.Content.Main", new TreeDropdownField("TargetContentID", "Choose a site to be forwarded to:", "SiteTree"));
		$fields->addFieldToTab("Root.Content.Photo", new ImageField('Photo'));
		return $fields;

class Box_Controller extends Page_Controller {}

Now I already get the correct page ID, but no idea how to handle this :-?

How do I get the URL of the page ID now and how can I filter the SiteTree that will be displayed in the dropdown?

Thanks for any help in advance!

Cheers, Mario


Forum Moderator, 5523 Posts

16 January 2010 at 5:16pm

Well to get the various properties of the TargetContent you use the built in ORM. Since you have a has_one relationship between your box and your TargetContent you can get the full TargetContent object using $TargetContent in the template or $this->TargetContent() in the PHP code.

Since you have the object you can then call all the usual SiteTree methods on them. Eg to get the URL in the template

<% if TargetContent %>$TargetContent.Link<% end_if %>


Community Member, 104 Posts

17 January 2010 at 1:07am

Edited: 17/01/2010 1:08am

I'm sure I did try that, but now it works perfect... maybe I was drunk ;-)

Thanks for the help!

What about filtering the DropDown in the backend?