Jump to:

23460 Posts in 18907 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Reusable Dropdown arrays

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: 1565 Views
  • Chris J
    Community Member
    10 Posts

    Reusable Dropdown arrays Link to this post


    I would like to store some arrays which I can call from any page type and DataObject.
    The arrays are just to populate dropdown elements in the CMS.

    Here is some example DataObject code which uses an array inside the DataObject:

    class TripDay extends DataObject {

       static $db = array(
          'Day' => 'Text',
          'DaySum' => 'Text',
          'DayDesc' => 'Text'
       static $has_one = array(
          'TripPages' => 'TripPage'
       function getCMSFields_forPopup() {
          $f = new FieldSet();
          $weekdays = array(
             '' => '(Please Select)',
             'Monday' => 'Monday',
             'Tuesday' => 'Tuesday',
             'Wednesday' => 'Wednesday',
             'Thursday' => 'Thursday',
             'Friday' => 'Friday',
             'Saturday' => 'Saturday',
             'Sunday' => 'Sunday'
          $f->push( new DropdownField('Day', 'Day of week', $weekdays) );
          $f->push( new TextField( 'DaySum', 'Summary of day' ) );
          $f->push( new TextAreaField('DayDesc', 'Full description', '15' ) );
          return $f;

    This works great but I would like to access this array and others from other DataObjects and Page types.

    What would be a good way to do that?

  • Willr
    Forum Moderator
    5497 Posts

    Re: Reusable Dropdown arrays Link to this post

    A great way to store that sort of information is using an Enum datafield. For example change your db array to

    static $db = array(
    'Day' => 'Enum("Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday")',

    This ensures that your data can only take on those values and you can get your day map by using

    // $foo will have a map of your enum
    $foo = singleton('TripDay')->dbObject('Day')->enumValues();

    You would then also define the default 'Please Select' text as the 6th agrument to the DropdownField. http://api.silverstripe.org/2.4/forms/fields-basic/DropdownField.html

  • Chris J
    Community Member
    10 Posts

    Re: Reusable Dropdown arrays Link to this post

    Hey thanks Willr that's great

    I didn't know about using


    like that. So I now have a dataObject which holds all of the dropdown element arrays that I need to reuse, and I can grab them from anywhere. Sweet

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.