使用 GridFieldDetailForm 修改字段时获取当前记录
get current record while modifying fields with GridFieldDetailForm
首先,我使用的是 SilverStripe 3.2 RC1。
如果你点击它,我需要修改一个网格字段项的显示字段。为此,我正在修改 'GridFieldDetailForm' 字段
$bundleGridForm = $bundleGridConfig->getComponentByType('GridFieldDetailForm');
$detailFormFields = FieldList::create(
TabSet::create(
'Root',
Tab::create(
'Haupt-Inhalt',
CheckboxSetField::create(
'ManyMany[AvailableVariations]',
'Verfügbare Variationen',
$HELP-NEEDED-HERE->VariationItems()->map()->toArray()
)
)
)
);
$bundleGridForm->setFields($detailFormFields);
如您所见,我只想显示一个 CheckboxSetField 作为源 $HELP-NEEDED-HERE->VariationItems()->map()->toArray()
我想使用当前选定/单击项目的现实 (VariationItems)。
问题是我不知道如何获得这种关系,因为 $this 当然是引用 class,我的网格字段在该位置,而不是单击的项目。
也许是 handleItem() function is what I need because it returns the GridFieldDetailForm_ItemRequest class where I can than call the getRecord() 函数。但是所有这一切都假定我从网格字段中获得了使用 handleItem() 函数的请求,而我完全不知道如何获取该记录。
如有任何帮助,我将不胜感激。
此致
回答我自己的问题以及同样被困在这里的任何人:
$bundleGridConfig
->removeComponentsByType('GridFieldAddNewButton');
$bundleGridForm = $bundleGridConfig->getComponentByType('GridFieldDetailForm');
// reset all fields, either you'r just adding new fields below
$bundleGridForm->setFields(FieldList::create());
$bundleGridForm->setItemEditFormCallback(function($form) {
$record = $form->getRecord();
// get the saved values
$availableVariations = $this->Items()->getExtraData('AvailableVariations', $record->ID);
$form->Fields()->push(
CheckboxSetField::create(
'VariationList',
'Verfügbare Variationen',
$record->VariationItems(),
// use the saved values as preset
explode(',', $availableVariations['AvailableVariations'])
)
);
// workaround for https://github.com/silverstripe/silverstripe-framework/issues/4067
$form->Fields()->push(
HiddenField::create('ManyMany[AvailableVariations]', 'Verfügbare Variationen', $availableVariations['AvailableVariations'])
);
});
问题是有一个 bug,它阻止 CheckboxSetFields 写入数据库。作为一种快速解决方法,我的 CheckboxSetField 只是一个 "pseudo-field" 来显示复选框,而将所选选项保存到数据库的字段是一个 HiddenField。
您现在要做的就是将所选选项解析为字符串,并在 CheckboxSetField 发生任何更改时将该字符串插入到隐藏字段中。
首先,我使用的是 SilverStripe 3.2 RC1。
如果你点击它,我需要修改一个网格字段项的显示字段。为此,我正在修改 'GridFieldDetailForm' 字段
$bundleGridForm = $bundleGridConfig->getComponentByType('GridFieldDetailForm');
$detailFormFields = FieldList::create(
TabSet::create(
'Root',
Tab::create(
'Haupt-Inhalt',
CheckboxSetField::create(
'ManyMany[AvailableVariations]',
'Verfügbare Variationen',
$HELP-NEEDED-HERE->VariationItems()->map()->toArray()
)
)
)
);
$bundleGridForm->setFields($detailFormFields);
如您所见,我只想显示一个 CheckboxSetField 作为源 $HELP-NEEDED-HERE->VariationItems()->map()->toArray()
我想使用当前选定/单击项目的现实 (VariationItems)。
问题是我不知道如何获得这种关系,因为 $this 当然是引用 class,我的网格字段在该位置,而不是单击的项目。
也许是 handleItem() function is what I need because it returns the GridFieldDetailForm_ItemRequest class where I can than call the getRecord() 函数。但是所有这一切都假定我从网格字段中获得了使用 handleItem() 函数的请求,而我完全不知道如何获取该记录。
如有任何帮助,我将不胜感激。
此致
回答我自己的问题以及同样被困在这里的任何人:
$bundleGridConfig
->removeComponentsByType('GridFieldAddNewButton');
$bundleGridForm = $bundleGridConfig->getComponentByType('GridFieldDetailForm');
// reset all fields, either you'r just adding new fields below
$bundleGridForm->setFields(FieldList::create());
$bundleGridForm->setItemEditFormCallback(function($form) {
$record = $form->getRecord();
// get the saved values
$availableVariations = $this->Items()->getExtraData('AvailableVariations', $record->ID);
$form->Fields()->push(
CheckboxSetField::create(
'VariationList',
'Verfügbare Variationen',
$record->VariationItems(),
// use the saved values as preset
explode(',', $availableVariations['AvailableVariations'])
)
);
// workaround for https://github.com/silverstripe/silverstripe-framework/issues/4067
$form->Fields()->push(
HiddenField::create('ManyMany[AvailableVariations]', 'Verfügbare Variationen', $availableVariations['AvailableVariations'])
);
});
问题是有一个 bug,它阻止 CheckboxSetFields 写入数据库。作为一种快速解决方法,我的 CheckboxSetField 只是一个 "pseudo-field" 来显示复选框,而将所选选项保存到数据库的字段是一个 HiddenField。
您现在要做的就是将所选选项解析为字符串,并在 CheckboxSetField 发生任何更改时将该字符串插入到隐藏字段中。