Silverstripe CMS BlogPost - DropdownField or SingleSelectField populate from Enum field

26 views Asked by At

I’m trying to add an option for title color in BlogPost summary view. I added the Enum field to database and I want to add the dropdown / select field under BlogPost title. I’m not sure which field type to use and how to set it properly.

class BlogPostExtension extends DataExtension
{
    private static $db = [
        'ArchiveDate' => 'Date',
        'TitleColor' => "Enum(array('black','red','green'))" // works only with this syntax
    ];

    private static $defaults = [
        'TitleColor' => 'black'
    ];


    public function updateCMSFields(FieldList $fields)
    {
        $fields->push(new DateField('ArchiveDate', 'Archive date'));
        $fields->push(new DropdownField('TitleColor','Color')); // doesn't populate the dropdown field
      //  $fields->push(new SelectField('TitleColor','Color'));   // cannot instantiate abstract class 'SelectField'
    }
}
1

There are 1 answers

1
user805528 On

If anyone is interested - I solved it like this:

public function updateCMSFields(FieldList $fields)
{
    $fields->push(new DateField('ArchiveDate', 'Archive date'));
    $fields->push(new DropdownField('TitleColor','Color', $this->getEnums()));
}

private function getEnums() {
    return singleton('SilverStripe\Blog\Model\BlogPost')->dbObject('TitleColor')->enumValues();
}