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 8, 2022
1 parent ed227e7 commit 2f9a244
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
9 changes: 5 additions & 4 deletions code/ModelAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -245,25 +246,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
7 changes: 4 additions & 3 deletions code/SecurityAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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')) {
Expand Down

0 comments on commit 2f9a244

Please sign in to comment.