Capitan Kamote
Capitan Kamote

Reputation: 85

Call to undefined method Silverstripe Gridfield

I need to have additional fields on a extended file but there is an error occur.

This will add a gallery tab with GridField which will allow the user to add images.

Not sure what is missing since it is working on other projects.

[Emergency] Uncaught Error: Call to undefined method MyProject\Extensions\BlogPostExtension::BlogGalleryImages()

<?php 


namespace MyProject\Extensions;


use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldEditButton;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
use SilverStripe\View\Requirements;
use MyProject\Model\BlogGalleryImage;

class BlogPostExtension extends DataExtension {

    private static $db = [
        'SortOrder' => 'Int'
    ];

    private static $has_many = [
        "BlogGalleryImages" => BlogGalleryImage::class,
    ];

    public function updateCMSFields(FieldList $fields) {
        // Add fields here

        $fields->removeByName("SortOrder");
        $options = $this->BlogGalleryImages();
        $gridFieldConfig = GridFieldConfig::create()->addComponents(
                new GridFieldAddNewButton('toolbar-header-right'),
                new GridFieldToolbarHeader(),
                new GridFieldSortableHeader(),
                new GridFieldDataColumns(),
                new GridFieldPaginator(80),
                new GridFieldDetailForm(),
                new GridFieldEditButton(),
                new GridFieldDeleteAction()
        );
        $itemsTable = new GridField("BlogGalleryImages","Gallery Image",$options,$gridFieldConfig);
        $fields->addFieldToTab('Root.Gallery',$itemsTable);

    }

}

Here is the Dataobject file that supposed to be call on BlogPostExtension Class

<?php 

namespace MyProject\Model;

use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\Image;
use SilverStripe\ORM\DataObject;

class BlogGalleryImage extends DataObject{

    private static $singular_name = 'Gallery Image';

    private static $db = array(
        'Title' => 'Varchar(256)',
        'SortOrder' => 'Int'
    );

    private static $has_one = array(
        'Image' => Image::class,
        'BlogPosts' => BlogPost::class,
    );

    private static $owns = [
        'Image',
    ];

    private static $summary_fields = array(
        'Thumbnail',
        'Title'
    );

    private static $table_name = 'MyProject_BlogGallery';


    private static $default_sort = "SortOrder ASC";

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

        $fields->removeByName("SortOrder");
        $fields->removeByName("BlogPostID");
        $fields->addFieldToTab("Root.Main", new TextField("Title","Title"));
        $fields->addFieldToTab("Root.Main", $uploadfield = UploadField::create("Image","Image"));
        $uploadfield->setFolderName("BlogGallery");

        return $fields;
    }

    public function getThumbnail(){
        if ($icon = $this->Image()){
            return $icon->CMSThumbnail();
        }else{
            return '(No Image)';
        }
    }

}

Upvotes: 2

Views: 750

Answers (1)

scrowler
scrowler

Reputation: 24405

You're calling an instance method on your extension which doesn't exist:

$options = $this->BlogGalleryImages();

This is actually a magic method which represents your has_many relationship "BlogGalleryImages" - this is scaffolded by DataObject. You need to refer to the extension's owner in order to access it:

$options = $this->getOwner()->BlogGalleryImages();

Upvotes: 1

Related Questions