Populate dropdown with enum values

Silverstripe Version: 4.*

I need two additional fields for general settings (TimeForClosingTab, ClosingTabOnOff). I get the fields but I don’t know how to populate the enum field correctly. The file is in app/src/extensions. What would be the correct way to define the class in singleton?

class CustomSiteConfig extends DataExtension
{

private static $db = [
    'TimeForClosingTab' => 'Int',
    'ClosingTabOnOff' => "Enum(array('On','Off'))"
];

public function updateCMSFields(FieldList $fields)
{
    $fields->addFieldToTab("Root.Main",
        new \SilverStripe\Forms\NumericField("TimeForClosingTab", "Time before closing new tab (s)"),
        new \SilverStripe\Forms\DropdownField('ClosingTabOnOff', 'Turn closing tab on / off',
            singleton('CustomSiteConfig')->dbObject('ClosingTabOnOff')->enumValues())
    );
}

The answer is in the yml :slight_smile:

singleton(‘\Silverstripe\SiteConfig\SiteConfig’)->dbObject(‘ClosingTabOnOff’)->enumValues())

For SiteConfig, you should get the SiteConfig object like so:
SiteConfig::current_site_config()

It’s already a singleton, there’s no need to create another singleton.

In this case though, assuming your CustomSiteConfig extension class in in fact only being used on SiteConfig, you should just use $this->getOwner()

1 Like