Silverstripe GridField dataFieldByName错误

时间:2019-12-14 23:50:28

标签: silverstripe-4

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

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;
        }

    }
}

2 个答案:

答案 0 :(得分:0)

至少在示例代码中,您在)函数调用中缺少右括号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')
)

答案 1 :(得分:0)

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

对于id order_number 1 78945 3 46646 类中的getCMSFields函数,我需要返回一个Section而不是一个字段。为此,我将功能更改为如下:

FieldList