Skip to content

Commit

Permalink
ENH: Prefer dependency injection for GridFieldComponents.
Browse files Browse the repository at this point in the history
GridFieldComponents packaged with silverstripe/framework are injectable
as of 4.11.0
Explicitly invoking the injector here instead of using `create()` allows backwards
compatability with framework < 4.11.0 while ensuring dependency
injection is still used from 4.11.0 onwards.
  • Loading branch information
GuySartorelli committed Feb 7, 2022
1 parent ed227e7 commit 202e855
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions code/ModelAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,25 +245,25 @@ protected function getGridFieldConfig(): GridFieldConfig
{
$config = GridFieldConfig_RecordEditor::create($this->config()->get('page_length'));

$exportButton = new GridFieldExportButton('buttons-before-left');
$exportButton = Injector::inst()->createWithArgs(GridFieldExportButton::class, ['buttons-before-left']);
$exportButton->setExportColumns($this->getExportFields());

$config
->addComponent($exportButton)
->addComponents(new GridFieldPrintButton('buttons-before-left'));
->addComponents(Injector::inst()->createWithArgs(GridFieldPrintButton::class, ['buttons-before-left']));

// Remove default and add our own filter header with extension points
// to retain API until deprecation in 5.0
$config->removeComponentsByType(GridFieldFilterHeader::class);
$config->addComponent(new GridFieldFilterHeader(
$config->addComponent(Injector::inst()->createWithArgs(GridFieldFilterHeader::class, [
false,
function ($context) {
$this->extend('updateSearchContext', $context);
},
function ($form) {
$this->extend('updateSearchForm', $form);
}
));
]));

if (!$this->showSearchForm ||
(is_array($this->showSearchForm) && !in_array($this->modelClass, $this->showSearchForm))
Expand Down
6 changes: 3 additions & 3 deletions code/SecurityAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public function getEditForm($id = null, $fields = null)
{
// Build gridfield configs
$memberListConfig = GridFieldConfig_RecordEditor::create()
->addComponent(new GridFieldExportButton('buttons-before-left'));
->addComponent(Injector::inst()->createWithArgs(GridFieldExportButton::class, ['buttons-before-left']));
$groupListConfig = GridFieldConfig_RecordEditor::create()
->addComponent(new GridFieldExportButton('buttons-before-left'));
->addComponent(Injector::inst()->createWithArgs(GridFieldExportButton::class, ['buttons-before-left']));

/** @var GridFieldDetailForm $detailForm */
$detailForm = $memberListConfig->getComponentByType(GridFieldDetailForm::class);
Expand All @@ -105,7 +105,7 @@ public function getEditForm($id = null, $fields = null)

/** @var GridFieldPageCount $groupPaginator */
$groupListConfig->removeComponentsByType(GridFieldPageCount::class);
$groupListConfig->addComponent(new GridFieldPageCount('buttons-before-right'));
$groupListConfig->addComponent(Injector::inst()->createWithArgs(GridFieldPageCount::class. ['buttons-before-right']));

// Add import capabilities. Limit to admin since the import logic can affect assigned permissions
if (Permission::check('ADMIN')) {
Expand Down

0 comments on commit 202e855

Please sign in to comment.