schellmax
schellmax

Reputation: 6094

SilverStripe GridField: add a 'type' dropdown to the 'add' button

I'm currently trying to manage items of a class as well as items of subclasses together in a GridField (think of a GridField containing 'Apple' and 'Banana' items, both subclasses from 'Fruit').

I already found how to switch the class of an item using some ClassName dropdown in the detail form (as it's done in the 'Page Type' dropdown contained in the 'Settings' tab for SiteTree items), and it's working fine.

What i'd like to do now is to already choose the subclass on creation, having a dropdown containing all classes next to the 'Add' button of the GridField. Unfortunately, the 'Add' button seems to call some 'EditForm' action in LeftAndMain, and i can't figure out where to go from here.

Does anyone know wheter this can be done extending some of GridFields' classes or the like?

Upvotes: 0

Views: 2502

Answers (2)

Mc User
Mc User

Reputation: 33

I've just done something similar in SS 3.1, its a 2 step process, but could be modified to be a 1 step.

In your super class add a dropdown for "ClassName" in getCMSFields() eg:

public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Main", 
        new DropdownField("ClassName", "Type", 
            array(
                "Apple" => "Apple", 
                "Banana" => "Banana",

            )
        )
    );

    return $fields;
}

Then when they click save on that page they it will show the get CMS fields for the subclass that was selected.

Upvotes: 0

ajshort
ajshort

Reputation: 3764

This functionality would be quite useful, so I've implemented it in my grid field extensions module: the commit in question.

Grid Field Extensions Module

To get it up and running, do something like the code below. It will automatically populate a dropdown with the parent class and any subclasses, and let the user choose which type to create.

$config = $grid->getConfig();
$config->removeComponentsByType('GridFieldAddNewButton');
$config->addComponent(new GridFieldAddNewMultiClass());

Upvotes: 2

Related Questions