【问题标题】:Silverstripe GridField dataFieldByName ErrorSilverstripe GridField dataFieldByName 错误
【发布时间】:2019-12-14 23:50:28
【问题描述】:

我正在尝试向我的 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;
        }

    }
}

【问题讨论】:

    标签: silverstripe-4


    【解决方案1】:

    嗯,至少在您的示例代码中,您在 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')
    )
    

    【讨论】:

    • 那个语法错误不在我的原始代码中,这是我删除不必要的代码时犯的错误。针对您的第二个想法,我认为我的语法错误让您感到困惑,因为我传递给FieldGroup 的不是“内容”,而是将'Content' 传递给addFieldToTab 函数。在我的原始代码中,FieldGroup 中有多个字段,但我忽略了它们。完整的sn-p代码见forum.silverstripe.org/t/gridfield-datafieldbyname-error/2528
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-11-03
      • 1970-01-01
      • 2022-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      相关资源
      最近更新 更多