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.

General Questions /

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

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

TreeDropdownField: default value


Go to End
Reply


10 Posts   3691 Views

Avatar
dacar

Community Member, 168 Posts

15 May 2010 at 12:54pm

Hi,

does anybody know, how to get the saved value from a TrreDropdownField. How can you use "objectForKey"?
Can anybody give an example?

new TreeDropdownField('BannerLinkInternID', 'Bitte eine interne Verlinkung wählen', 'SiteTree', 'ID', 'Title', true);

Greetings, Carsten.

Avatar
Willr

Forum Moderator, 5513 Posts

15 May 2010 at 10:33pm

does anybody know, how to get the saved value from a TrreDropdownField. How can you use "objectForKey"?
Can anybody give an example?

Not exactly sure what you mean by objectForKey but what I think your getting at is how you get the BannerLinkIntern object. Well with a TreedropdownField you should have setup a has_one relation on that page of 'BannerLinkIntern' => 'SiteTree' so to access the object in templates you can use $BannerLinkIntern to access the related page or in the PHP $this->BannerLinkIntern() to get the object.

Avatar
dacar

Community Member, 168 Posts

16 May 2010 at 12:49am

Hi Willr,

thats all clear and works fine. But if i choose "home" from the list (for e.g.) and save the date to the db, then the page (backend) is reloaded and the value of the list is: "(Choose or type search)".

What do i have to do to show "Home" as the selected value after saving the data to DB?

objectforkey: [URL]http://api.silverstripe.com/2.4/forms/fields-relational/TreeDropdownField.html#methodobjectForKey[/URL]

Thanks for your reply, Carsten.

Avatar
Willr

Forum Moderator, 5513 Posts

16 May 2010 at 11:44am

Sounds like the 'home' value isn't saving. Have you got a has_one relationship setup correctly between this page and sitetree and ran a /dev/build. You should after building a correct has_one relationship a column in your database 'BannerLinkInternID' if you don't have this column then the treedropdownfield cannot save its value.

Avatar
jebbench

Community Member, 7 Posts

30 May 2010 at 4:34am

I'm having the same problem, the field isn't saving back to the database. I can't get the link from my template and the field shows Choose a page rather than the page I selected.

The code I'm using is:

class Module extends Page {
static $db = array(
);
static $has_one = array(
'Photo' => 'Image',
'Header' => 'Image',
'LinkedPage' => 'SiteTree'
);
static $defaults = array(
"ShowInMenus" => 0,
);
static $can_be_root = false;
static $default_parent = 'HomePage';

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Main", new ImageField('Header'));
$fields->addFieldToTab("Root.Content.Main", new ImageField('Photo'));
$fields->addFieldToTab('Root.Content.Main', new TreeDropdownField('LinkedPage', 'Page to link to', 'SiteTree'));
$fields->removeFieldFromTab('Root.Content.Main', 'MenuTitle');
$fields->removeByName('Metadata');
return $fields;
}

}

and in the template

$LinkedPage.Link

Am I doing something wrong or is this a bug?

Cheers,

James Bench

Avatar
Willr

Forum Moderator, 5513 Posts

30 May 2010 at 11:25am

You need to use LinkedPageID instead of LinkedPage in your treedropdownfield constructor.

Avatar
jebbench

Community Member, 7 Posts

31 May 2010 at 10:23pm

Cheers, that's fixed it :-)

Avatar
Bambii7

Community Member, 254 Posts

9 July 2010 at 12:03pm

I'm a clouts, again I don't know what I'm doing wrong.
Same as above not getting id saved into the has_oneID field in the database.

   static $has_one = array(
      "LinkTo" => "SiteTree"
   );

in getCMSFields_forPopup I'm adding and to and returning

$fields->addFieldToTab( 'Root.Main', new TreeDropdownField("LinkToID", "Link To This Page","SiteTree"));

I'm also trying SimpleTreeDropdownField as well, neither work.
Doing a little homepage captions dataobject, there's only about 3, easier enough to swap out manually. But I like using Silver Stripe to the EXTREME! :P
p.s. using SS 2.4

Go to Top