1

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

invictus
  • 825
  • 1
  • 9
  • 33

2 Answers2

3

the problem is that the config object is tied to the gridfield as soon as you add it. that means that you need 2 config objects, currently you only have one. you can either create a second one, or clone the first one:

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

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

$categoryField = new GridField('ShopCategories', 'Kategorien', $this->ShopCategories(), $gridFieldConfig2);
$fields->addFieldToTab('Root.Shop Kategorien', $categoryField);
Zauberfisch
  • 3,870
  • 18
  • 25
3

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());
Adrexia
  • 61
  • 2