Silverstripe GridField dataFieldByName 错误

Silverstripe GridField dataFieldByName Error

我正在尝试向我的 Silverstripe CMS 添加一个简单的 GridField,仅包含 HTMLEditorFields。我正在使用 GridFieldConfig_RecordEditor。当我单击 "Add Section" 时,出现内部服务器错误。然后如果我刷新页面,我得到以下错误:

Uncaught BadMethodCallException: Object->__call(): the method 'dataFieldByName' does not exist on 'SilverStripe\Forms\HTMLEditor\HTMLEditorField'

我不知道是什么原因造成的。有人知道为什么会这样吗?

这是我的代码 Page.php:

<?php

namespace {

    use SilverStripe\CMS\Model\SiteTree;
    use Silverstripe\Forms\CheckboxField;
    use Silverstripe\Forms\FieldGroup;
    use Silverstripe\Forms\HTMLEditor\HTMLEditorField;
    use SilverStripe\Forms\GridField\GridField;
    use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
    use SilverStripe\ORM\DataObject;

    class Section extends DataObject {
        private static $db = [
            'SectionContent' => 'HTMLText'
        ];

        private static $has_one = [
            'Page' => Page::class
        ];

        public function getCMSFields() {
            return HTMLEditorField::create('SectionContent');
        }
    }

    class Page extends SiteTree {
        private static $db = [
            'IncludeSections' => 'Boolean'
        ];

        private static $has_many = [
            'Sections' => Section::class
        ];

        public function getCMSFields() {
            $fields = parent::getCMSFields();

            $fields->addFieldToTab(
                'Root.Main',
                FieldGroup::create(
                    CheckboxField::create("IncludeSections")
                ), 'Content'
            );

            if ($this->IncludeSections) {
                $fields->addFieldToTab('Root.Main',
                   $grid = GridField::create(
                       'Sections',
                       'Sections in this page. Seperated by boxes.',
                       $this->Sections(),
                       GridFieldConfig_RecordEditor::create()
                   )
                );
            }

            return $fields;
        }

    }
}

嗯,至少在您的示例代码中,您在 addFieldToTab 函数调用中缺少右括号 )

$fields->addFieldToTab(
        'Root.Main',
        FieldGroup::create(
            CheckboxField::create("IncludeSections"),
            'Content'
        ); // Should be one `)` before this.

固定:

$fields->addFieldToTab(
        'Root.Main',
        FieldGroup::create(
            CheckboxField::create("IncludeSections"),
            'Content'
        )
   );

此外,如果您在修复该问题后遇到异常,问题可能与您作为第二个参数传递给 FieldGroup 的构造函数的内容有关。

文档:

/**
     * Create a new field group.
     *
     * Accepts any number of arguments.
     *
     * @param mixed $titleOrField Either the field title, list of fields, or first field
     * @param mixed ...$otherFields Subsequent fields or field list (if passing in title to $titleOrField)
     */
    public function __construct($titleOrField = null, $otherFields = null) {...}

传递表单字段实例可能值得一试。 (未测试)

FieldGroup::create(
    CheckboxField::create("IncludeSections"),
    $fields->dataFieldByName('Content')
)

我在 https://forum.silverstripe.org/t/gridfield-datafieldbyname-error/2528 收到了问题的答案。

对于 Section class 中的 getCMSFields 函数,我需要 return 一个 FieldList 而不是单个字段。为此,我将函数更改为如下:

public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Main', HTMLEditorField::create('SectionContent'));

    return $fields;
}