我想使用GridField来查看和创建新的子页面。父是DocumentHolder,子是Document。两者都扩展了SiteTree。当我点击"添加文档" (由网格生成的按钮),填写字段并确认表单,忽略父页面并在root中创建页面。我使用DataObject时效果很好。代码如下所示:
class DocumentHolder extends SiteTree
{
private static $allowed_children = array(
'Document'
);
private static $default_child = "Document";
public function getCMSFields()
{
$fields = parent::getCMSFields();
$gridField = new GridField('Documents', 'Documents', SiteTree::get('Document')->filter('ParentID', $this->ID), GridFieldConfig_RecordEditor::create());
$fields->addFieldToTab("Root.Uploads", $gridField);
return $fields;
}
}
class Document extends SiteTree
{
private static $db = array(
);
private static $has_one = array(
);
}
感谢您的帮助。
答案 0 :(得分:0)
由于SiteTree已经设置了与其子页面的关系,您也可以使用它!由于allowed_children将只是文档,请尝试这样做:
$gridField = new GridField('Documents', 'Documents', $this->Children(), GridFieldConfig_RecordEditor::create());
答案 1 :(得分:0)
我之前在我的holderpage模块上遇到了这个问题。您需要默认设置ParentID。这是两个策略;
您可以在子类上使用populateDefaults
。 E.g。
class Document extends SiteTree
{
private static $default_parent = 'DocumentHolder';
private static $can_be_root = false;
public function populateDefaults(){
parent::populateDefaults();
$this->ParentID = DataObject::get_one(self::$default_parent)->ID;
}
...
或者您可以使用自定义GridFieldDetailForm
实施或updateItemEditForm
回调操作网格字段中的记录。
<?php
class MyGridFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest
{
public function ItemEditForm()
{
$form = parent::ItemEditForm();
if (! $this->record->exists() && $this->record->is_a('SiteTree')) {
$parent_page = $this->getController()->currentPage();
if ($parent_page && $parent_page->exists()) {
$this->record->ParentID = $parent_page->ID;
// update URLSegment @TODO perhaps more efficiently?
$field = $this->record->getCMSFields()->dataFieldByName('URLSegment');
$form->Fields()->replaceField('URLSegment', $field);
}
}
return $form;
}
}
这更复杂,虽然它允许我创建一个轻松的模块/插件(https://github.com/briceburg/silverstripe-holderpage)