【问题标题】:Add new Page using GridField - creates the child in root folder使用 GridField 添加新页面 - 在根文件夹中创建子页面
【发布时间】:2014-09-03 14:07:48
【问题描述】:

我想使用 GridField 来查看和创建新的子页面。父级是 DocumentHolder,子级是 Document。两者都扩展了 SiteTree。当我单击“添加文档”(网格生成的按钮)时,填写字段并确认表单,父页面被忽略,页面在根目录下创建。当我使用 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(
    );
}

感谢您的帮助。

【问题讨论】:

    标签: silverstripe


    【解决方案1】:

    由于 SiteTree 已经与其子页面建立了关系,您不妨使用它!由于 allowed_children 只会是文档,请尝试以下操作:

    $gridField = new GridField('Documents', 'Documents', $this->Children(), GridFieldConfig_RecordEditor::create());
    

    【讨论】:

    • 嗨艾略特,感谢您的回答,我之前也尝试过,但结果相同......
    【解决方案2】:

    我之前在使用 holderpage 模块时遇到了这个问题。您需要默认设置 ParentID。这里有两种策略;

    您可以在子类上使用populateDefaults。例如

    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),但这更复杂

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      相关资源
      最近更新 更多