NEW Extend new AbstractGridFieldComponent class (#332)

This makes this module's `GridFieldComponent` classes `Injectable`, and allows any future enhancements in the new abstract class to automatically apply without requiring additional changes in this module.

The class is introduced in silverstripe/framework 4.11.0 so the dependency constraint needs to be updated.

Also update docs to encourage use of dependency injection.
This commit is contained in:
Guy Sartorelli 2022-03-04 10:12:24 +13:00 committed by GitHub
parent 8c4e924bfa
commit ec93c994f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 20 deletions

View File

@ -21,7 +21,7 @@
"require": { "require": {
"php": "^7.4 || ^8.0", "php": "^7.4 || ^8.0",
"silverstripe/vendor-plugin": "^1.0", "silverstripe/vendor-plugin": "^1.0",
"silverstripe/framework": "^4.10" "silverstripe/framework": "^4.11"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^9.5", "phpunit/phpunit": "^9.5",

View File

@ -9,7 +9,7 @@ existing records than a basic autocomplete. It uses the search context construct
class to provide the search form. class to provide the search form.
```php ```php
$grid->getConfig()->addComponent(new GridFieldAddExistingSearchButton()); $grid->getConfig()->addComponent(GridFieldAddExistingSearchButton::create());
``` ```
Inline Editing Inline Editing
@ -19,17 +19,17 @@ This example replaces the default data columns component with an inline editable
default add new button with one that adds new records inline. default add new button with one that adds new records inline.
```php ```php
$grid = new GridField( $grid = GridField::create(
'ExampleGrid', 'ExampleGrid',
'Example Grid', 'Example Grid',
$this->Items(), $this->Items(),
GridFieldConfig::create() GridFieldConfig::create()
->addComponent(new GridFieldButtonRow('before')) ->addComponent(GridFieldButtonRow::create('before'))
->addComponent(new GridFieldToolbarHeader()) ->addComponent(GridFieldToolbarHeader::create())
->addComponent(new GridFieldTitleHeader()) ->addComponent(GridFieldTitleHeader::create())
->addComponent(new GridFieldEditableColumns()) ->addComponent(GridFieldEditableColumns::create())
->addComponent(new GridFieldDeleteAction()) ->addComponent(GridFieldDeleteAction::create())
->addComponent(new GridFieldAddNewInlineButton()) ->addComponent(GridFieldAddNewInlineButton::create())
); );
``` ```
@ -39,7 +39,7 @@ inline editing component. By default field scaffolding will be used.
```php ```php
$grid->getConfig()->getComponentByType(GridFieldEditableColumns::class)->setDisplayFields(array( $grid->getConfig()->getComponentByType(GridFieldEditableColumns::class)->setDisplayFields(array(
'FirstField' => function($record, $column, $grid) { 'FirstField' => function($record, $column, $grid) {
return new TextField($column); return TextField::create($column);
}, },
'SecondField' => array( 'SecondField' => array(
'title' => 'Custom Title', 'title' => 'Custom Title',
@ -69,7 +69,7 @@ use SilverStripe\Forms\GridField\GridFieldAddNewButton;
$grid->getConfig() $grid->getConfig()
->removeComponentsByType(GridFieldAddNewButton::class) ->removeComponentsByType(GridFieldAddNewButton::class)
->addComponent(new GridFieldAddNewMultiClass()); ->addComponent(GridFieldAddNewMultiClass::create());
``` ```
Orderable Rows Orderable Rows
@ -82,10 +82,10 @@ the relationship.
```php ```php
// Basic usage, defaults to "Sort" for the sort field. // Basic usage, defaults to "Sort" for the sort field.
$grid->getConfig()->addComponent(new GridFieldOrderableRows()); $grid->getConfig()->addComponent(GridFieldOrderableRows::create());
// Specifying the sort field. // Specifying the sort field.
$grid->getConfig()->addComponent(new GridFieldOrderableRows('SortField')); $grid->getConfig()->addComponent(GridFieldOrderableRows::create('SortField'));
``` ```
By default, when you create a new item, it is created with a sort order of "0" - that is, it is added By default, when you create a new item, it is created with a sort order of "0" - that is, it is added
@ -118,7 +118,7 @@ To use this component you should remove the original paginator component first:
```php ```php
$gridField->getConfig() $gridField->getConfig()
->removeComponentsByType('GridFieldPaginator') ->removeComponentsByType('GridFieldPaginator')
->addComponent(new GridFieldConfigurablePaginator()); ->addComponent(GridFieldConfigurablePaginator::create());
``` ```
You can configure the page sizes with the configuration system. Note that merging is the default strategy, so to replace You can configure the page sizes with the configuration system. Note that merging is the default strategy, so to replace
@ -133,7 +133,7 @@ Config::inst()->update('GridFieldConfigurablePaginator', 'default_page_sizes', a
You can also override these at call time: You can also override these at call time:
```php ```php
$paginator = new GridFieldConfigurablePaginator(100, array(100, 200, 500)); $paginator = GridFieldConfigurablePaginator::create(100, array(100, 200, 500));
$paginator->setPageSizes(array(200, 500, 1000)); $paginator->setPageSizes(array(200, 500, 1000));
$paginator->setItemsPerPage(500); $paginator->setItemsPerPage(500);

View File

@ -2,6 +2,7 @@
namespace Symbiote\GridFieldExtensions; namespace Symbiote\GridFieldExtensions;
use SilverStripe\Forms\GridField\AbstractGridFieldComponent;
use SilverStripe\Forms\GridField\GridField_HTMLProvider; use SilverStripe\Forms\GridField\GridField_HTMLProvider;
use SilverStripe\Forms\GridField\GridField_URLHandler; use SilverStripe\Forms\GridField\GridField_URLHandler;
use SilverStripe\ORM\SS_List; use SilverStripe\ORM\SS_List;
@ -11,7 +12,9 @@ use SilverStripe\View\ArrayData;
* A modal search dialog which uses search context to search for and add * A modal search dialog which uses search context to search for and add
* existing records to a grid field. * existing records to a grid field.
*/ */
class GridFieldAddExistingSearchButton implements GridField_HTMLProvider, GridField_URLHandler class GridFieldAddExistingSearchButton extends AbstractGridFieldComponent implements
GridField_HTMLProvider,
GridField_URLHandler
{ {
private static $allowed_actions = array( private static $allowed_actions = array(
@ -111,6 +114,6 @@ class GridFieldAddExistingSearchButton implements GridField_HTMLProvider, GridFi
public function handleSearch($grid, $request) public function handleSearch($grid, $request)
{ {
return new GridFieldAddExistingSearchHandler($grid, $this); return GridFieldAddExistingSearchHandler::create($grid, $this);
} }
} }

View File

@ -5,6 +5,7 @@ namespace Symbiote\GridFieldExtensions;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\AbstractGridFieldComponent;
use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridField_HTMLProvider; use SilverStripe\Forms\GridField\GridField_HTMLProvider;
use SilverStripe\Forms\GridField\GridField_SaveHandler; use SilverStripe\Forms\GridField\GridField_SaveHandler;
@ -21,7 +22,9 @@ use Exception;
/** /**
* Builds on the {@link GridFieldEditableColumns} component to allow creating new records. * Builds on the {@link GridFieldEditableColumns} component to allow creating new records.
*/ */
class GridFieldAddNewInlineButton implements GridField_HTMLProvider, GridField_SaveHandler class GridFieldAddNewInlineButton extends AbstractGridFieldComponent implements
GridField_HTMLProvider,
GridField_SaveHandler
{ {
/** /**
* @skipUpgrade * @skipUpgrade

View File

@ -9,6 +9,7 @@ use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\GridField\AbstractGridFieldComponent;
use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridField_HTMLProvider; use SilverStripe\Forms\GridField\GridField_HTMLProvider;
use SilverStripe\Forms\GridField\GridField_URLHandler; use SilverStripe\Forms\GridField\GridField_URLHandler;
@ -23,7 +24,9 @@ use Exception;
* By default the list of classes that are createable is the grid field's model class, and any * By default the list of classes that are createable is the grid field's model class, and any
* subclasses. This can be customised using {@link setClasses()}. * subclasses. This can be customised using {@link setClasses()}.
*/ */
class GridFieldAddNewMultiClass implements GridField_HTMLProvider, GridField_URLHandler class GridFieldAddNewMultiClass extends AbstractGridFieldComponent implements
GridField_HTMLProvider,
GridField_URLHandler
{ {
/** /**
* @skipUpgrade * @skipUpgrade

View File

@ -2,6 +2,7 @@
namespace Symbiote\GridFieldExtensions; namespace Symbiote\GridFieldExtensions;
use SilverStripe\Forms\GridField\AbstractGridFieldComponent;
use SilverStripe\Forms\GridField\GridField_HTMLProvider; use SilverStripe\Forms\GridField\GridField_HTMLProvider;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData; use SilverStripe\View\ArrayData;
@ -9,7 +10,7 @@ use SilverStripe\View\ArrayData;
/** /**
* A simple header which displays column titles. * A simple header which displays column titles.
*/ */
class GridFieldTitleHeader implements GridField_HTMLProvider class GridFieldTitleHeader extends AbstractGridFieldComponent implements GridField_HTMLProvider
{ {
public function getHTMLFragments($grid) public function getHTMLFragments($grid)