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

Dropdownfield doesn't set selected item


Go to End


4 Posts   930 Views

Avatar
quanto

Community Member, 91 Posts

22 March 2013 at 1:29am

Edited: 22/03/2013 1:29am

Hi,

I'm trying to insert a DropDownfield into a CMSfield popup in Silverstripe 2.4.

The right value is inserted into the DB after saving. However, the Dropdownfield doesn't stay to the selected item but returns to the first. My code:

class Link extends DataObject{
  static $db = array(
    'Descript'=> 'Text',
    'ExternURL'=> 'Text',
    'InternURL'=> 'Varchar'
    
  ); 

 
  static $has_one = array( 
    'Pagina' => 'SiteTree'
  );
   
  public function getCMSFields_forPopup() {
    $fields = parent::getCMSFields();
    $fields = new FieldSet( 
      new TextField('Descript', 'Description'),
      new TextField('ExternURL', 'External link (including http://)'),
      new DropdownField('InternURL','OR Internal link:', DataObject::get("SiteTree")->toDropdownMap('ID', 'Title', '(Select one)', true)) 
    ); 
    
    return $fields; 
  }
}

Anyone a solution?

Avatar
quanto

Community Member, 91 Posts

23 March 2013 at 2:32am

Found the solution: I got a function "getInternURL()", but that's not possible.

Avatar
Willr

Forum Moderator, 5523 Posts

26 March 2013 at 8:10pm

InternURL, your managed DropdownField should rather be in your has_one array if you want to store a relationship between Link and a SiteTree object.

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

new DropdownField('InternURLID','OR Internal link:', DataObject::get("SiteTree")->map('ID', 'Title')))

Avatar
quanto

Community Member, 91 Posts

28 March 2013 at 2:55am

Thanks, it works for me! However, it took me an hour to find out the ID after the InternURL.

Even easier now:

$fields->addFieldToTab('Root.Main', new TreeDropdownField('InternURL', 'Link Blok 2', 'SiteTree'));