From 2f9a2446a31cdd447134e2d2adf8ffe57bab595f Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 8 Feb 2022 12:16:17 +1300 Subject: [PATCH] ENH: Prefer dependency injection for GridFieldComponents. 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. --- code/ModelAdmin.php | 9 +++++---- code/SecurityAdmin.php | 7 ++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/code/ModelAdmin.php b/code/ModelAdmin.php index 5bc28c16a..19b0b3c06 100644 --- a/code/ModelAdmin.php +++ b/code/ModelAdmin.php @@ -6,6 +6,7 @@ use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\Core\Convert; +use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\BulkLoader; use SilverStripe\Dev\Deprecation; use SilverStripe\Forms\CheckboxField; @@ -245,17 +246,17 @@ 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); @@ -263,7 +264,7 @@ function ($context) { function ($form) { $this->extend('updateSearchForm', $form); } - )); + ])); if (!$this->showSearchForm || (is_array($this->showSearchForm) && !in_array($this->modelClass, $this->showSearchForm)) diff --git a/code/SecurityAdmin.php b/code/SecurityAdmin.php index 92150c197..26f1b3b6e 100755 --- a/code/SecurityAdmin.php +++ b/code/SecurityAdmin.php @@ -5,6 +5,7 @@ use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\Core\Convert; +use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\GridField\GridField; @@ -94,9 +95,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); @@ -105,7 +106,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')) {