Jump to:

5554 Posts in 1742 Topics by 1227 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Creating a user selection in the backend using TreeDropdownField

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

Page: 1
Go to End
Author Topic: 1014 Views
  • theGermaniac
    Avatar
    Community Member
    82 Posts

    Creating a user selection in the backend using TreeDropdownField Link to this post

    Hi,

    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

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Creating a user selection in the backend using TreeDropdownField Link to this post

    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 %>

  • theGermaniac
    Avatar
    Community Member
    82 Posts

    Re: Creating a user selection in the backend using TreeDropdownField Link to this post

    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?

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