Silverstripe CMS BlogPost - DropdownField or SingleSelectField populated from enum field
P粉573943755
P粉573943755 2023-09-16 16:02:00
0
1
689

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粉573943755
P粉573943755

reply all(1)
P粉113938880

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();
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!