Silverstripe 4 getCMSFields_forPopup 和 GridField

Silverstripe 4 getCMSFields_forPopup and GridField

时隔多年再次拾起。我不能在 cms 弹出组件中使用 gridfield 吗?这里我有 Ingredient 实体,我想将数据库中的 Ingredients 添加到 Recipe 实体。连个简单的都没有出现

Recipe.php

    ...

    private static $db = [
        'Title' => 'Varchar',
        'Description' => 'Text',
    ];

    private static $has_one = [];

    private static $many_many = [
        'Ingredients' => Ingredient::class,
    ];


    public function getCMSFields_forPopup()
    {
        $gridConfig = GridFieldConfig_RelationEditor::create()->addComponents(
            new GridFieldDeleteAction('unlinkrelation')
        );

        $grid = GridField::create(
            'Ingredients',
            'Ingredients',
            $this->Ingredients(),
            $gridConfig,
        );

        $fields = FieldList::create(
            TextField::create('Title'),
            TextareaField::create('Description'),
            $grid
        );

        // or maybe something like..
        // $fields->addFieldToTab('Main', 'Ingredients', 'Ingredients', $grid);


        return $fields;
    }

getCMSFields_forPopup 在 Silverstripe 4 或 Silverstripe 3 中不存在。这是在 Silverstripe 2 中。

试试 getCMSFields

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

    $ingredientsFieldConfig = GridFieldConfig_RelationEditor::create();

    $ingredientsField = GridField::create(
        'Ingredients',
        'Ingredients',
        $this->Ingredients(),
        $ingredientsFieldConfig
    );

    $fields->addFieldToTab('Root.Main', $ingredientsFieldConfig);

    return $fields;
}