如何在 silverstripe 中将图像排序为 many_many 的一部分?

How to sort images as part of a many_many in silverstripe?

我在主页上为 $slideImages 创建了一个 $many_many 数组。经过大量阅读和尝试后,我仍然无法告诉 CMS 我希望图像出现在模板中的顺序。我相信默认情况下它们是按上传日期排序的。

我可以创建网格字段,但我似乎无法获得用于输入排序编号的文本字段。现在我刚刚完成了一条引导我到 gridFieldComponent 的 rabbit trail,但我收到一个错误并且文档没有帮助我。

use SilverStripe\Assets\Image;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\GridField\GridFieldComponent;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\GridField\GridField;

class HomePage extends Page
{
    private static $db = [];

    private static $has_one = [];

    private static $many_many = [
        'SliderImage'=>Image::Class
    ];

    private static $owns = [
        'SliderImage'
    ];
    private static $many_many_extraFields= [
        'SliderImage'=>array(            
            'Sort'=>'Int'
        )         
    ];

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

        $fields->addFieldToTab('Root.Attachments', UploadField::create('SliderImage', 'Sllider Images')->setFolderName('HomePageSlides'));

        $gridFieldConfig = GridFieldConfig_RelationEditor::create()->addComponents(

            new GridFieldComponent(TextField('Sort'))
    );

        $gridField = new GridField("SliderImage", "Slider Image", $this->SliderImage()->sort('Sort'), $gridFieldConfig);

        $fields->addFieldToTab("Root.Attachments", $gridField);

        return $fields;
    }
}

我得到的错误是:

"Uncaught Error: Cannot instantiate interface SilverStripe\Forms\GridField\GridFieldComponent"

根据 wmk 的评论,您在 Sort 字段的 TextField 初始化中缺少 new::create

应该是:

new GridFieldComponent(new TextField('Sort'))

或者更好:

GridFieldComponent::create(TextField::create('Sort'))