Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Make all GridField components injectable (using abstract class) #10204

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 34 additions & 32 deletions docs/en/02_Developer_Guides/03_Forms/Field_types/04_GridField.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tabular data in a format that is easy to view and modify. It can be thought of a
```php
use SilverStripe\Forms\GridField\GridField;

$field = new GridField($name, $title, $list);
$field = GridField::create($name, $title, $list);
```

[hint]
Expand Down Expand Up @@ -45,7 +45,7 @@ class Page extends SiteTree
$fields = parent::getCMSFields();

$fields->addFieldToTab('Root.Pages',
new GridField('Pages', 'All pages', SiteTree::get())
GridField::create('Pages', 'All pages', SiteTree::get())
);

return $fields;
Expand Down Expand Up @@ -81,7 +81,7 @@ class Page extends SiteTree
$fields = parent::getCMSFields();

$fields->addFieldToTab('Root.Pages',
$grid = new GridField('Pages', 'All pages', SiteTree::get())
$grid = GridField::create('Pages', 'All pages', SiteTree::get())
);

// GridField configuration
Expand Down Expand Up @@ -115,7 +115,7 @@ use SilverStripe\Forms\GridField\GridFieldDataColumns;
$config = GridFieldConfig::create();

// add a component
$config->addComponent(new GridFieldDataColumns());
$config->addComponent(GridFieldDataColumns::create());

// Update the GridField with our custom configuration
$gridField->setConfig($config);
Expand All @@ -128,16 +128,16 @@ before another component by passing the second parameter.
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
use SilverStripe\Forms\GridField\GridFieldDataColumns;

$config->addComponent(new GridFieldFilterHeader(), GridFieldDataColumns::class);
$config->addComponent(GridFieldFilterHeader::create(), GridFieldDataColumns::class);
```

We can add multiple components in one call.


```php
$config->addComponents(
new GridFieldDataColumns(),
new GridFieldToolbarHeader()
GridFieldDataColumns::create(),
GridFieldToolbarHeader::create()
);
```

Expand Down Expand Up @@ -191,12 +191,12 @@ $config = GridFieldConfig_Base::create();
$gridField->setConfig($config);

// Is the same as adding the following components..
// .. new GridFieldToolbarHeader()
// .. new GridFieldSortableHeader()
// .. new GridFieldFilterHeader()
// .. new GridFieldDataColumns()
// .. new GridFieldPageCount('toolbar-header-right')
// .. new GridFieldPaginator($itemsPerPage)
// .. GridFieldToolbarHeader::create()
// .. GridFieldSortableHeader::create()
// .. GridFieldFilterHeader::create()
// .. GridFieldDataColumns::create()
// .. GridFieldPageCount::create('toolbar-header-right')
// .. GridFieldPaginator::create($itemsPerPage)
```

### GridFieldConfig_RecordViewer
Expand All @@ -223,8 +223,8 @@ $config = GridFieldConfig_RecordViewer::create();
$gridField->setConfig($config);

// Same as GridFieldConfig_Base with the addition of
// .. new GridFieldViewButton(),
// .. new GridFieldDetailForm()
// .. GridFieldViewButton::create(),
// .. GridFieldDetailForm::create()
```

### GridFieldConfig_RecordEditor
Expand All @@ -248,9 +248,9 @@ $config = GridFieldConfig_RecordEditor::create();
$gridField->setConfig($config);

// Same as GridFieldConfig_RecordViewer with the addition of
// .. new GridFieldAddNewButton(),
// .. new GridFieldEditButton(),
// .. new GridFieldDeleteAction()
// .. GridFieldAddNewButton::create(),
// .. GridFieldEditButton::create(),
// .. GridFieldDeleteAction::create()
```

### GridFieldConfig_RelationEditor
Expand Down Expand Up @@ -287,9 +287,9 @@ $config = GridFieldConfig::create();
$config->addComponent();

$config->addComponents(
new GridFieldDataColumns(),
new GridFieldEditButton(),
new GridField_ActionMenu()
GridFieldDataColumns::create(),
GridFieldEditButton::create(),
GridField_ActionMenu::create()
);

// Update the GridField with our custom configuration
Expand All @@ -308,8 +308,8 @@ The `GridFieldDetailForm` component drives the record viewing and editing form.
use SilverStripe\Forms\GridField\GridFieldDetailForm;

$form = $gridField->getConfig()->getComponentByType(GridFieldDetailForm::class);
$form->setFields(new FieldList(
new TextField('Title')
$form->setFields(FieldList::create(
TextField::create('Title')
));
```

Expand Down Expand Up @@ -369,13 +369,13 @@ class Player extends DataObject
$teamFields->addFieldToTab(
'Root.Main',
// The "ManyMany[<extradata-name>]" convention
new TextField('ManyMany[Position]', 'Current Position')
TextField::create('ManyMany[Position]', 'Current Position')
);

$config = GridFieldConfig_RelationEditor::create();
$config->getComponentByType(GridFieldDetailForm::class)->setFields($teamFields);

$gridField = new GridField('Teams', 'Teams', $this->Teams(), $config);
$gridField = GridField::create('Teams', 'Teams', $this->Teams(), $config);
$fields->findOrMakeTab('Root.Teams')->replaceField('Teams', $gridField);
}

Expand Down Expand Up @@ -405,8 +405,8 @@ bottom right of the table.
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldPrintButton;

$config->addComponent(new GridFieldButtonRow('after'));
$config->addComponent(new GridFieldPrintButton('buttons-after-right'));
$config->addComponent(GridFieldButtonRow::create('after'));
$config->addComponent(GridFieldPrintButton::create('buttons-after-right'));
```

### Creating your own Fragments
Expand All @@ -417,12 +417,13 @@ create an area rendered before the table wrapped in a simple `<div>`.


```php
use SilverStripe\Forms\GridField\AbstractGridFieldComponent;
use SilverStripe\Forms\GridField\GridField_HTMLProvider;

class MyAreaComponent implements GridField_HTMLProvider
class MyAreaComponent extends AbstractGridFieldComponent implements GridField_HTMLProvider
{

public function getHTMLFragments( $gridField)
public function getHTMLFragments($gridField)
{
return [
'before' => '<div class="my-area">$DefineFragment(my-area)</div>'
Expand All @@ -442,12 +443,13 @@ Now you can add other components into this area by returning them as an array fr


```php
use SilverStripe\Forms\GridField\AbstractGridFieldComponent;
use SilverStripe\Forms\GridField\GridField_HTMLProvider;

class MyShareLinkComponent implements GridField_HTMLProvider
class MyShareLinkComponent extends AbstractGridFieldComponent implements GridField_HTMLProvider
{

public function getHTMLFragments( $gridField)
public function getHTMLFragments($gridField)
{
return [
'my-area' => '<a href>...</a>'
Expand All @@ -461,7 +463,7 @@ Your new area can also be used by existing components, e.g. the [GridFieldPrintB


```php
new GridFieldPrintButton('my-area');
GridFieldPrintButton::create('my-area');
```

## Creating a Custom GridFieldComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ title: Create a GridField Component
summary: Customise your GridField with a variety of add-ons.
icon: table
---
A single component often uses a number of interfaces.
A single component often uses a number of interfaces. It is good practice for your custom
components to subclass the `AbstractGridFieldComponent` class to ensure they behave the same
way as built-in components (e.g. are `Injectable`).

### GridField_HTMLProvider

Expand Down Expand Up @@ -50,4 +52,4 @@ has a list of URL's that it can handle and the GridField passes request on to UR
Examples:

- A pop-up form for editing a record's details.
- JSON formatted data used for javascript control of the gridfield.
- JSON formatted data used for javascript control of the gridfield.
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ below:


```php
use SilverStripe\Forms\GridField\AbstractGridFieldComponent;
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
use SilverStripe\Forms\GridField\GridField_ActionProvider;
use SilverStripe\Forms\GridField\GridField_FormAction;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Control\Controller;

class GridFieldCustomAction implements GridField_ColumnProvider, GridField_ActionProvider
class GridFieldCustomAction extends AbstractGridFieldComponent implements GridField_ColumnProvider, GridField_ActionProvider
{

public function augmentColumns($gridField, &$columns)
Expand Down Expand Up @@ -114,12 +115,12 @@ manipulating the `GridFieldConfig` instance if required.
```php
// option 1: creating a new GridField with the CustomAction
$config = GridFieldConfig::create();
$config->addComponent(new GridFieldCustomAction());
$config->addComponent(GridFieldCustomAction::create());

$gridField = new GridField('Teams', 'Teams', $this->Teams(), $config);
$gridField = GridField::create('Teams', 'Teams', $this->Teams(), $config);

// option 2: adding the CustomAction to an existing GridField
$gridField->getConfig()->addComponent(new GridFieldCustomAction());
$gridField->getConfig()->addComponent(GridFieldCustomAction::create());
```

For documentation on adding a Component to a `GridField` created by `ModelAdmin`
Expand Down Expand Up @@ -177,13 +178,14 @@ For an action to be included in the action menu dropdown, which appears on each
## Basic GridFieldCustomAction boilerplate implementing GridField_ActionMenuItem

```php
use SilverStripe\Forms\GridField\AbstractGridFieldComponent;
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
use SilverStripe\Forms\GridField\GridField_ActionProvider;
use SilverStripe\Forms\GridField\GridField_ActionMenuItem;
use SilverStripe\Forms\GridField\GridField_FormAction;
use SilverStripe\Control\Controller;

class GridFieldCustomAction implements GridField_ColumnProvider, GridField_ActionProvider, GridField_ActionMenuItem
class GridFieldCustomAction extends AbstractGridFieldComponent implements GridField_ColumnProvider, GridField_ActionProvider, GridField_ActionMenuItem
{

public function getTitle($gridField, $record, $columnName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class MyAdmin extends ModelAdmin
$context = parent::getSearchContext();

if($this->modelClass == 'Product') {
$context->getFields()->push(new CheckboxField('q[ExpensiveOnly]', 'Only expensive stuff'));
$context->getFields()->push(CheckboxField::create('q[ExpensiveOnly]', 'Only expensive stuff'));
}

return $context;
Expand Down Expand Up @@ -362,7 +362,7 @@ class MyAdmin extends ModelAdmin
{
$config = parent::getGridFieldConfig();

$config->addComponent(new GridFieldFilterHeader());
$config->addComponent(GridFieldFilterHeader::create());

return $config;
}
Expand Down Expand Up @@ -397,7 +397,7 @@ class MyAdmin extends ModelAdmin

// modify the list view.
if ($this->modelClass === Product::class) {
$config->addComponent(new GridFieldFilterHeader());
$config->addComponent(GridFieldFilterHeader::create());
}

return $config;
Expand Down Expand Up @@ -425,7 +425,7 @@ class ModelAdminExtension extends Extension
{
public function updateGridFieldConfig(GridFieldConfig &$config)
{
$config->addComponent(new GridFieldFilterHeader());
$config->addComponent(GridFieldFilterHeader::create());
}
}
```
Expand Down Expand Up @@ -470,7 +470,7 @@ class MyAdmin extends ModelAdmin
$gridField = $form->Fields()->fieldByName($gridFieldName);

// modify the list view.
$gridField->getConfig()->addComponent(new GridFieldFilterHeader());
$gridField->getConfig()->addComponent(GridFieldFilterHeader::create());

return $form;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest:
You can also override this for a specific `GridField` instance when using the `GridFieldConfig_RecordEditor` constructor:

```php
$grid = new GridField(
$grid = GridField::create(
"pages",
"All Pages",
SiteTree::get(),
Expand Down
10 changes: 10 additions & 0 deletions src/Forms/GridField/AbstractGridFieldComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace SilverStripe\Forms\GridField;

use SilverStripe\Core\Injector\Injectable;

abstract class AbstractGridFieldComponent implements GridFieldComponent
{
use Injectable;
}
8 changes: 4 additions & 4 deletions src/Forms/GridField/GridField.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public function performReadonlyTransformation()

// If the edit button has been removed, replace it with a view button
if ($hadEditButton && !$copyConfig->getComponentByType(GridFieldViewButton::class)) {
$copyConfig->addComponent(new GridFieldViewButton);
$copyConfig->addComponent(GridFieldViewButton::create());
}

return $copy;
Expand Down Expand Up @@ -295,7 +295,7 @@ public function setConfig(GridFieldConfig $config)
$this->config = $config;

if (!$this->config->getComponentByType(GridState_Component::class)) {
$this->config->addComponent(new GridState_Component());
$this->config->addComponent(GridState_Component::create());
}

return $this;
Expand Down Expand Up @@ -440,7 +440,7 @@ private function initState(): void
public function FieldHolder($properties = [])
{
$this->extend('onBeforeRenderHolder', $this, $properties);

$columns = $this->getColumns();

$list = $this->getManipulatedList();
Expand Down Expand Up @@ -655,7 +655,7 @@ public function FieldHolder($properties = [])
$tableAttributes,
$header . "\n" . $footer . "\n" . $body
);

$message = Convert::raw2xml($this->getMessage());
if (is_array($message)) {
$message = $message['message'];
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/GridField/GridFieldAddExistingAutocompleter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* For easier setup, have a look at a sample configuration in
* {@link GridFieldConfig_RelationEditor}.
*/
class GridFieldAddExistingAutocompleter implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator, GridField_URLHandler
class GridFieldAddExistingAutocompleter extends AbstractGridFieldComponent implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator, GridField_URLHandler
{

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/GridField/GridFieldAddNewButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Only returns a button if {@link DataObject->canCreate()} for this record
* returns true.
*/
class GridFieldAddNewButton implements GridField_HTMLProvider
class GridFieldAddNewButton extends AbstractGridFieldComponent implements GridField_HTMLProvider
{

protected $targetFragment;
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/GridField/GridFieldButtonRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* This row provides two new HTML fragment spaces: 'toolbar-header-left' and
* 'toolbar-header-right'.
*/
class GridFieldButtonRow implements GridField_HTMLProvider
class GridFieldButtonRow extends AbstractGridFieldComponent implements GridField_HTMLProvider
{

protected $targetFragment;
Expand Down
Loading