search

Home  >  Q&A  >  body text

Silverstripe CMS BlogPost - DropdownField or SingleSelectField populated from enum field

I'm trying to add a header color option to the BlogPost summary view. I added an enum field to the database and I want to add a dropdown/select field under the BlogPost header. I'm not sure which field type to use and how to set it up correctly.

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'
    }
}

P粉573943755P粉573943755442 days ago836

reply all(1)I'll reply

  • P粉113938880

    P粉1139388802023-09-17 11:38:55

    If anyone is interested - this is how I solved it:

    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();
    }

    reply
    0
  • Cancelreply