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

*SOLVED* Default static value in dynamically populated DropdownField


Go to End


3 Posts   1465 Views

Avatar
_Matt

Community Member, 29 Posts

25 February 2013 at 7:50am

Is it possible to add a default static value to a dynamically populated DropdownField in admin?

In my fieldset I have this code:

new DropdownField('BlogCategory', 'Choose a Category', Dataobject::get("BlogCategory")->map("ID", "Title"))

I tried following the instructions in this article (http://chillburn.com.au/blog/adding-a-please-select-option-to-a-silverstripe-dropdownfield-when-using-a-dataobject-todropdownmap/), but I just keep getting server errors. I suspect it's because the article was written pre SS3 and things have changed now.

Can anyone help?

Avatar
kinglozzer

Community Member, 187 Posts

25 February 2013 at 10:16pm

Make sure you put your site in dev mode: in _config.php add the line

Director::set_environment_type("dev");

You should then be able to see the errors properly. If you don't see them, try refreshing the page or looking in your javascript console (download Firebug for Firefox if you don't know what that is).

Do you actually have any 'BlogCategory' DataObjects? If not, that would likely cause an error.

Also, you mention adding the DropdownField to the admin area, but the instructions are for adding it to the front-end of the site.

Avatar
_Matt

Community Member, 29 Posts

25 February 2013 at 10:49pm

Thanks for the info about setting dev mode in _config.php - I've done it when using _ss-environment.php, but not in config before.

After a bit more digging around (and chatting with a dev friend) I managed to sort it out. There's a function in the DropdownField class called setEmptyString(), which does exactly that.

My code is basically for a 'Latest Posts' widget. I want the user to be able to filter the latest posts by Category (I'm using the blogcategories module: https://github.com/IOTI/silverstripe-blogcategories). However, I wanted the first value in the dropdown to be 'All Posts'.

For anyone who is interested here's the code I'm using:

public function getCMSFields() {
$categoryDropdown = new DropdownField(
'BlogCategory',
'Choose a Category',
Dataobject::get("BlogCategory")->map('ID', 'Title')
);
return new FieldList(
new TextField('WidgetTitle', 'Widget Title'),
new NumericField("NumberToShow", "Number to Show"),
$categoryDropdown->setEmptyString('All Categories'),
new TextField('ReadArticleLinkTitle', 'Read Article Link Title')
);
}

... And if you look at the attached screen grab you'll see what this looks like in Admin.

Attached Files