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

Default value using map()


Go to End


3 Posts   710 Views

Avatar
Vlad Belfort

Community Member, 55 Posts

25 February 2016 at 5:45am

Edited: 25/02/2016 6:13am

How do I get a default value to appear when using the map method? What used to work on v2.x doesnt on 3.2

Example:

In my Model I have:

    public function getListOfSites(){

        if($sites = DataObject::get('Site'))
            return $sites->map('ID', 'Title', 'All');

    }

Then in Page.php I want to add the sites to the settings tab like so

$fields->addFieldToTab('Root.Settings', new DropdownField('Site', 'Select a site that will see this article', Site::getListOfSites()));

Where "All" is was what appeared as the default value in a drop down menu in v2.x but no longer works... What else can I do?

Avatar
Devlin

Community Member, 344 Posts

26 February 2016 at 3:51am

Edited: 26/02/2016 3:56am

The parameters $emptyString and $sort of DataObjectSet->map() got removed. Please use DropdownField->setEmptyString() instead.

$site = new DropdownField('Site', 'Select a site that will see this article', array());
$site->setEmptyString('All');
$fields->addFieldToTab('Root.Settings', $site);

Avatar
Vlad Belfort

Community Member, 55 Posts

26 February 2016 at 4:21am

Edited: 26/02/2016 4:22am

Thanks! Spent so much time trying to figure out the correct way of doing it. Your solution worked great