Search code examples
phpsilverstripesilverstripe-4

Silverstripe 4 getCMSFields_forPopup and GridField


Picking this up again after many years. Can I not use gridfield within the cms popup component? Here I have Ingredient entity and am wanting to add Ingredients from the db to a Recipe entity. Even a simple one doesn't appear.

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;
    }

Solution

  • getCMSFields_forPopup does not exist in Silverstripe 4 or Silverstripe 3. This was in Silverstripe 2.

    Try getCMSFields instead.

    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;
    }