我想在Silverstripe 3.2中使用Userforms插件默认添加一些字段 我想我找到了在添加字段时添加字段的功能。在Gridfield中按下按钮,但我不确定,我不知道如何通过调用一个简单的函数添加一个formfield类型(日期输入)。 这是完整的功能:
public function getFieldEditorGrid() {
Requirements::javascript(USERFORMS_DIR . '/javascript/FieldEditor.js');
$fields = $this->owner->Fields();
$this->createInitialFormStep(true);
$editableColumns = new GridFieldEditableColumns();
$fieldClasses = singleton('EditableFormField')->getEditableFieldClasses();
$editableColumns->setDisplayFields(array(
'ClassName' => function($record, $column, $grid) use ($fieldClasses) {
if($record instanceof EditableFormField) {
return $record->getInlineClassnameField($column, $fieldClasses);
}
},
'Title' => function($record, $column, $grid) {
if($record instanceof EditableFormField) {
return $record->getInlineTitleField($column);
}
}
));
$config = GridFieldConfig::create()
->addComponents(
$editableColumns,
new GridFieldButtonRow(),
GridFieldAddClassesButton::create('EditableTextField')
->setButtonName(_t('UserFormFieldEditorExtension.ADD_FIELD', 'Add Field'))
->setButtonClass('ss-ui-action-constructive'),
GridFieldAddClassesButton::create('EditableFormStep')
->setButtonName(_t('UserFormFieldEditorExtension.ADD_PAGE_BREAK', 'Add Page Break')),
GridFieldAddClassesButton::create(array('EditableFieldGroup', 'EditableFieldGroupEnd'))
->setButtonName(_t('UserFormFieldEditorExtension.ADD_FIELD_GROUP', 'Add Field Group')),
new GridFieldEditButton(),
new GridFieldDeleteAction(),
new GridFieldToolbarHeader(),
new GridFieldOrderableRows('Sort'),
new GridFieldDetailForm()
);
$fieldEditor = GridField::create(
'Fields',
_t('UserDefinedForm.FIELDS', 'Fields'),
$fields,
$config
)->addExtraClass('uf-field-editor');
return $fieldEditor;
}
答案 0 :(得分:2)
我们可以在第一次保存页面后调用onAfterWrite
来设置默认字段。
class CustomFormPage extends UserDefinedForm {
public function onAfterWrite() {
if (!$this->Fields() || !$this->Fields()->exists()) {
$nameField = new EditableTextField();
$nameField->Name = 'Name';
$nameField->Title = 'Name';
$nameField->ParentID = $this->ID;
$nameField->Required = true;
$nameField->CustomErrorMessage = 'Please enter your name.';
$nameField->write();
$dateField = new EditableDateField();
$dateField->Name = 'Date';
$dateField->Title = 'Date';
$dateField->ParentID = $this->ID;
$dateField->Required = true;
$dateField->CustomErrorMessage = 'Please enter this date.';
$dateField->write();
}
parent::onAfterWrite();
}
}