Skip to content

Commit

Permalink
DOCS Encourage injection for GridField and GridFieldComponents.
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Jan 16, 2022
1 parent 29da8f6 commit 3edc3e0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 45 deletions.
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

0 comments on commit 3edc3e0

Please sign in to comment.