我正在嘗試在 BlogPost 摘要視圖中新增標(biāo)題顏色選項(xiàng)。 我將枚舉字段添加到資料庫(kù)中,並且我想在 BlogPost 標(biāo)題下添加下拉/選擇字段。 我不確定要使用哪種字段類(lèi)型以及如何正確設(shè)置它。
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' } }
如果有人有興趣 - 我是這樣解決的:
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(); }