我正在嘗試在 BlogPost 摘要視圖中新增標題顏色選項。 我將枚舉字段添加到資料庫中,並且我想在 BlogPost 標題下添加下拉/選擇字段。 我不確定要使用哪種字段類型以及如何正確設置它。
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粉1139388802023-09-17 11:38:55
如果有人有興趣 - 我是這樣解決的:
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(); }