Jump to:

3395 Posts in 977 Topics by 853 members

Template Questions

SilverStripe Forums » Template Questions » new TreeDropdownField returns id of the page

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

Page: 1
Go to End
Author Topic: 1312 Views
  • NtM
    Avatar
    Community Member
    39 Posts

    new TreeDropdownField returns id of the page Link to this post

    Here is y code:

    static $db = array(
          'Bucket1_Link' => 'Text',
       );
    public function getCMSFields()
       {
          $fields = parent::getCMSFields();   
          $fields->addFieldToTab('Root.Content.Bucket1', new TreeDropdownField('Bucket1_Link', 'Choose a page', 'SiteTree'));
    return $fields;
    }

    in the template I use the variable $Bucket1_Link, whhich is id of the page. But I need the URL of the page. How would I get it?
    thank you!

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: new TreeDropdownField returns id of the page Link to this post

    Well for TreeDropdownFields you normally save the field as a 'relationship' with a page. Change the

    static $db = array(
    'Bucket1_Link' => 'Text',
    );

    To a $has_one

    static $has_one = array(
    'Bucket1' => 'SiteTree',
    );

    And slight tweak to your getCMSFields...

    $fields->addFieldToTab('Root.Content.Bucket1', new TreeDropdownField('Bucket1_Link', 'Choose a page', 'SiteTree'));

    Change to

    $fields->addFieldToTab('Root.Content.Bucket1', new TreeDropdownField('Bucket1ID', 'Choose a page', 'SiteTree'));

    Rebuild your database and refresh the page, resave the value then in the template you can now use $Bucket1 as a Page object so you can do $Bucket1.Link

  • NtM
    Avatar
    Community Member
    39 Posts

    Re: new TreeDropdownField returns id of the page Link to this post

    Thanks a lot!

    I did like this:

    in the control of the page I defined a function:

    public function page_url_by_id_bucket1(){
          $pageID = $this->Bucket1_Link;
        $obj = DataObject::get_by_id('SiteTree', $pageID);
          $URL = $obj->URLSegment;
          return $URL;
    }

    and in the template I write:

    "<a href="$page_url_by_id_bucket1">Link</a> "

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: new TreeDropdownField returns id of the page Link to this post

    Thats the other way you can do it usually in SS we prefer passing around relationships and objects rather then just strings as its alot more flexible.

    Watch out in your version - if $this->Bucket1_Link gets unset (eg a client or someone doesn't set it) that function will throw a error as it won't be able to get the $obj then you're calling $obj->URLSegment on it. You'll need to add a check to make sure $obj exists before calling any methods on it.

  • NtM
    Avatar
    Community Member
    39 Posts

    Re: new TreeDropdownField returns id of the page Link to this post

    The only problem with your method is that it doesn't save the selected page from the tree. It always shows the message "(Choose)"

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