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 12, 2022
1 parent 375c319 commit c85b99d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
12 changes: 6 additions & 6 deletions code/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,12 @@ public function getReportField()
$items = $this->sourceRecords($params, null, null);

$gridFieldConfig = GridFieldConfig::create()->addComponents(
new GridFieldButtonRow('before'),
new GridFieldPrintButton('buttons-before-left'),
new GridFieldExportButton('buttons-before-left'),
new GridFieldSortableHeader(),
new GridFieldDataColumns(),
new GridFieldPaginator()
Injector::inst()->createWithArgs(GridFieldButtonRow::class, ['before']),
Injector::inst()->createWithArgs(GridFieldPrintButton::class, ['buttons-before-left']),
Injector::inst()->createWithArgs(GridFieldExportButton::class, ['buttons-before-left']),
Injector::inst()->create(GridFieldSortableHeader::class),
Injector::inst()->create(GridFieldDataColumns::class),
Injector::inst()->create(GridFieldPaginator::class)
);
/** @var GridField $gridField */
$gridField = GridField::create('Report', null, $items, $gridFieldConfig);
Expand Down
9 changes: 5 additions & 4 deletions code/ReportAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridField;
Expand Down Expand Up @@ -230,11 +231,11 @@ public function getEditForm($id = null, $fields = null)
// List all reports
$fields = new FieldList();
$gridFieldConfig = GridFieldConfig::create()->addComponents(
new GridFieldSortableHeader(),
new GridFieldDataColumns(),
new GridFieldFooter()
Injector::inst()->create(GridFieldSortableHeader::class),
Injector::inst()->create(GridFieldDataColumns::class),
Injector::inst()->create(GridFieldFooter::class)
);
$gridField = new GridField('Reports', false, $this->Reports(), $gridFieldConfig);
$gridField = GridField::create('Reports', false, $this->Reports(), $gridFieldConfig);
/** @var GridFieldDataColumns $columns */
$columns = $gridField->getConfig()->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDataColumns');
$columns->setDisplayFields(array(
Expand Down

0 comments on commit c85b99d

Please sign in to comment.