Search code examples
silverstripedata-objects

Silverstripe 3 -Multiple Gridfields on one Page


i want to add multiple gridfields to one pagetype. At the moment I'm doing it like this

        $gridFieldConfig = GridFieldConfig::create()->addComponents(
        new GridFieldToolbarHeader(),
        new GridFieldAddNewButton('toolbar-header-right'),
        new GridFieldSortableHeader(),
        new GridFieldDataColumns(),
        new GridFieldPaginator(10),
        new GridFieldEditButton(),
        new GridFieldDeleteAction(),
        new GridFieldDetailForm()
    );

    $sliderField = new GridField('Slides', 'Slider', $this->Slides(), $gridFieldConfig);
    $fields->addFieldToTab('Root.Slider', $sliderField);

    $categoryField = new GridField('ShopCategories', 'Kategorien', $this->ShopCategories(), $gridFieldConfig);
    $fields->addFieldToTab('Root.Shop Kategorien', $categoryField);

It works but the problem is that i got the same "add blablabla object" title for both.

How can i fix this without using multiple gridFieldConfigs?

Thx in Advance


Solution

  • It seems that if you use the same config, the details are shared by both. However, there are a few default configs set-up you could use instead.

    • GridFieldConfig_RelationEditor
    • GridFieldConfig_RecordEditor
    • GridFieldConfig_RecordViewer
    • GridFieldConfig_Base

    All of which extend the gridfield config.

    So you could do this, for instance:

    $sliderField = new GridField(
                'Slides', 
                'Slider', 
                $this->Slides(), 
                GridFieldConfig_RelationEditor::create()
    );
    

    If you wish to create your own custom config you can write a class that extends GridFieldConfig in the same manner:

    class GridFieldConfig_Custom extends GridFieldConfig {
    /**
     *
     * @param int $itemsPerPage - How many items per page should show up
     */
    public function __construct($itemsPerPage=null) {
    
        $this->addComponent(new GridFieldToolbarHeader());
        $this->addComponent(new GridFieldAddNewButton('toolbar-header-right'));
        $this->addComponent(new GridFieldSortableHeader());
        $this->addComponent(new GridFieldDataColumns());
        $this->addComponent(new GridFieldPaginator(10));
        $this->addComponent(new GridFieldEditButton());
        $this->addComponent(new GridFieldDeleteAction());
        $this->addComponent(new GridFieldDetailForm());
    }
    }
    

    And then:

    $sliderField = new GridField(
                'Slides', 
                'Slider', 
                $this->Slides(), 
                GridFieldConfig_Custom::create());
    
    $categoryField = new GridField(
               'ShopCategories', 
               'Kategorien', 
               $this->ShopCategories(), 
               GridFieldConfig_Custom::create());