GridField dataFieldByName Error

Silverstripe Version: 4.4.x-dev

Question: Getting Uncaught BadMethodCallException: Object->__call(): the method ‘dataFieldByName’ does not exist on ‘SilverStripe\Forms\HTMLEditor\HTMLEditorField’. How can I fix this?

I am attempting to create a simple gridfield, the only content being HTMLEditorFields. I am using the GridFieldConfig_RecordEditor. When I click “Add section” I get an Internal Server Error. Then, if I refresh the page I get the following error:

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

I have no clue how to solve this. Does anybody have any ideas?

Here is my Page.php code:

<?php

namespace {

    use SilverStripe\CMS\Model\SiteTree;
    use Silverstripe\Forms\CheckboxField;
    use Silverstripe\Forms\FieldGroup;
    use Silverstripe\Forms\HTMLEditor\HTMLEditorField;
    use SilverStripe\Forms\DateField;    
    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 = [
	    'NoParagraphSpace' => 'Boolean',
	    'TableHorzLine' => 'Boolean',
	    'TableVertLine' => 'Boolean',
	    'EmbedPDF' => 'Boolean',
	    'IncludeSidebar' => 'Boolean',
	    'IncludeDate' => 'Boolean',
	    'Date' => 'Date',
	    'IncludeSections' => 'Boolean'
	];

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

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

	    $fields->addFieldToTab(
		'Root.Main',
		FieldGroup::create(
		    CheckboxField::create("NoParagraphSpace", "No Space between paragraphs"),
		    CheckboxField::create("TableHorzLine", "Horizontal lines between table rows"),
		    CheckboxField::create("TableVertLine", "Vertical lines between table columns"),
		    CheckboxField::create("EmbedPDF", "Embed PDFs directly into the page"),
		    CheckboxField::create("IncludeSidebar", "Include a sidebar in this page"),
		    CheckboxField::create("IncludeDate", "Include the date the page was last updated at the bottom of the page"),
		    CheckboxField::create("IncludeSections", "Seperate the page into sections, each in their own box.")
		)->setTitle('Options'),
		'Content'
	    );
	    
 	    if ($this->IncludeDate) {
 		$fields->addFieldToTab('Root.Main', DateField::create('Date', 'Enter the date the page was most recently updated'), 'Metadata');
 	    }
	    
 	    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;
	}
	
    }
}

Your getCMSFields() method needs to return a Fieldlist, and currently you’re just returning a single field. If you change it to match the logic of the one in the Page class, then hopefully you should have better results.

eg. Get the $fields from the parent, add your new fields with addFieldToTab() and return the $fields from the method.

As an aside, it’s recommended practice to have each PHP file contain only a single class, so it’s probably worth splitting the Section class into its own file.

1 Like

Thank you so much! This fixed it!

Regarding your aside, yes I will do that. I only did it to make things simpler while I was getting things working.

1 Like