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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Choosing a URL from sitetree


Go to End


30 Posts   16403 Views

Avatar
reececropley

Community Member, 11 Posts

19 March 2013 at 2:15am

Hey,

Im using the SiteTree to allow the user to choose a page on the site to create a link.

The Page.php has this code

public static $has_one = array(
		
		"CaptionOneLink" => "SiteTree"
	);

public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab("Root.SliderText", new TreeDropdownField('CaptionOneLink', 'Caption 1 Link', 'SiteTree'));
        return $fields;
 }

The template file page.ss has the following:

      <a href="$CaptionOneLink.Link">Link to page</a>

In the admin section I can see the dropdown, with the pages in their. However when I select one and save it display "(Choose One)" rather than the selected page name.

When I load the page there is no link in the <a> anchor tag. I checked the database and the ID it has entered for CaptionOneLink is 0.

Is there something I'm missing? Or Documentation you can link to?

Avatar
marc79

Community Member, 65 Posts

19 April 2013 at 8:53am

Hi,

I have been able to add a SiteTree dropdown to my DataObject pop up however I only want to show specific page types.

Is it possible to filter the pages displayed in the dropdown based on $ClassName?

This is what I have so far:

$fields->push( new TreeDropdownField('PackageType','Package Page', 'SiteTree', 'ID'));

There are many different packages available and these are split up into various types of packages which I want to be displayed on the pages of the site. Because packages can only be displayed on pages with a $ClassName of 'PackagePage' these are the only pages I want to display in the dropdown, make it easier for my client.

Any help greatly appreciated.

thanks

Avatar
Arbee

Community Member, 31 Posts

19 August 2013 at 2:02am

Marc, this is what I need to know too. Did you find the answer?

Avatar
kinglozzer

Community Member, 187 Posts

19 August 2013 at 8:15pm

Edited: 19/08/2013 8:16pm

Assuming you're on 3.x, not sure about 2.x:

$tree = new TreeDropdownField('PackageType','Package Page', 'SiteTree', 'ID');
$tree->setFilterFunction(function($obj) {
	if ($obj->ClassName == "MyClass") {
		return false;
	} else {
		return true;
	}
});

This will hide any options with the class name 'MyClass'. Note that if you hide the 'MyClass' page type, its child pages will also be hidden. I've submitted a pull request to disable instead of hide them, so that child pages will still be accessible - though it'd only make it in to 3.1.1 at the earliest: https://github.com/silverstripe/silverstripe-framework/pull/2037

Avatar
Arbee

Community Member, 31 Posts

19 August 2013 at 11:30pm

Oops no sorry, I am using 2.4. Is it doable in there?

Avatar
kinglozzer

Community Member, 187 Posts

20 August 2013 at 3:51am

Looking at the API (http://api.silverstripe.org/2.4/class-TreeDropdownField.html#_setFilterFunction) it should work in the exact same way

Go to Top