Jump to:

23489 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » *SOLVED* Default static value in dynamically populated DropdownField

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

Page: 1
Go to End
Author Topic: 556 Views
  • _Matt
    Community Member
    25 Posts

    *SOLVED* Default static value in dynamically populated DropdownField Link to this post

    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?

  • kinglozzer
    Community Member
    142 Posts

    Re: *SOLVED* Default static value in dynamically populated DropdownField Link to this post

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


    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.

  • _Matt
    Community Member
    25 Posts

    Re: *SOLVED* Default static value in dynamically populated DropdownField Link to this post

    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(
    '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
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.