2

I have an extra Data field added to a many to many relation, and I have no Idea how to edit it.

Feature

private static $belongs_many_many = [
        'Model' => Model::class
    ];

Model

private static $many_many = [
        'Features' => Feature::class,
    ];

private static $many_many_extraFields = [
    'Features' => [
        'SortOrder' => 'Int',
        'Data' => 'Varchar'
    ]
];



-- / --

$features = Feature::get();
$searchFeaturesButton = new GridFieldAddExistingSearchButton();
$searchFeaturesButton->setSearchList($features);
$featureConfig = GridFieldConfig_RelationEditor::create();
$featureConfig->removeComponentsByType([GridFieldAddExistingAutocompleter::class, GridFieldEditButton::class, GridFieldAddNewButton::class]);
$featureConfig->addComponent(new GridFieldOrderableRows());
$featureConfig->addComponent($searchFeaturesButton);
$featureGrid = GridField::create('Features', 'Features', $this->Features(), $featureConfig);
$field->addFieldToTab('Root.Main', $featureGrid);

-- / --


How can I view Data as a column in my GridField and have it editable as well?

Sorting works fine as it's managed by GridfieldOrderableRows()

Zubair
  • 715
  • 1
  • 9
  • 15

1 Answers1

3

In SilverStripe 3 and 4 we can use the SilverStripe GridField Extensions module GridFieldEditableColumns feature to edit many_many_extraFields data.

Here is an example of how to do this:

// Features field existing search button
$featuresGridFieldSearchButton = new GridFieldAddExistingSearchButton();
$featuresGridFieldSearchButton->setSearchList(Feature::get());

// Features field editable columns
$featuresGridFieldEditableColumns = new GridFieldEditableColumns();
$featuresGridFieldEditableColumns->setDisplayFields([
    'Title' => [
        'title' => 'Title',
        'field' => ReadonlyField::class,
    ],
    'Data' => [
        'title' => 'Data',
        'field' => TextField::class,
    ],
]);

// Features field config including base GridFieldConfig_RelationEditor components, custom search button, editable columns and orderable rows
$featuresGridFieldConfig = GridFieldConfig::create();
$featuresGridFieldConfig->addComponent(new GridFieldButtonRow('before'));
$featuresGridFieldConfig->addComponent($featuresGridFieldSearchButton);
$featuresGridFieldConfig->addComponent(new GridFieldToolbarHeader());
$featuresGridFieldConfig->addComponent(new GridFieldTitleHeader());
$featuresGridFieldConfig->addComponent($featuresGridFieldEditableColumns);
$featuresGridFieldConfig->addComponent(new GridFieldDeleteAction(true));
$featuresGridFieldConfig->addComponent(new GridFieldOrderableRows());
$featuresGridFieldConfig->addComponent(new GridFieldPageCount('toolbar-header-right'));
$featuresGridFieldConfig->addComponent(new GridFieldPaginator());
$featuresGridFieldConfig->addComponent(new GridFieldDetailForm());

$featuresGridField = GridField::create(
    'Features',
    'Features',
    $this->Features(),
    $featuresGridFieldConfig
);

$fields->addFieldToTab('Root.Features', $featuresGridField);
3dgoo
  • 15,716
  • 6
  • 46
  • 58
  • Works in the backend, but somehow not showing in template. I can loop through $Features just fine but it won't get $Data while looping through $Features. the method 'forTemplate' does not exist on 'Feature' – Zubair Jun 21 '19 at 19:40
  • 1
    I have tested accessing `$many_many_extraFields` through the front end and they work in my test. For the extra fields to work we need to go through `Model` and use `Model's` `Features` relationship call. – 3dgoo Jun 24 '19 at 22:35