Skip to content

Commit

Permalink
DOC Updated Nested GridField docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed May 27, 2024
1 parent 079bec6 commit 328dfe7
Showing 1 changed file with 147 additions and 5 deletions.
152 changes: 147 additions & 5 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,148 @@ Nested GridFields
The `GridFieldNestedForm` component allows you to nest GridFields in the UI. It can be used with `DataObject` subclasses
with the `Hierarchy` extension, or by specifying the relation used for nesting.

Here is a small example of basic use of Nested GridField.

```php
class MyAdmin extends ModelAdmin
{
...

public function getEditForm($id = null, $fields = null)
{
$form = parent::getEditForm($id, $fields);

$grid = $form->Fields()->dataFieldByName(MyObject::class);
// Add Nested GridField to main GridField
$grid->getConfig()->addComponent(GridFieldNestedForm::create());

return $form;
}
}
```

There are several ways to use Nested GridField. The implementation depends on the type of data you will be using in your GridField.
For instance, if there is a DataObject that has the [`Hierarchy`](api:SilverStripe\ORM\Hierarchy\Hierarchy) extension, you can use the following approach.

As an example, here we can use a typical hierarchy of Security Groups, where another Group can serve as a parent.

```php
class MySecurityAdminExtension extends Extension
{
public function updateGridFieldConfig($config)
{
if ($this->owner->getModelClass() === Group::class) {
$config->addComponent(GridFieldNestedForm::create()->setRelationName('Members'));
}
}
}
```

```yml
SilverStripe\Admin\SecurityAdmin:
extensions:
- MyApp\MyExtensions\MySecurityAdminExtension
```
Another way to use Nested GridField together with DataObjects that do not have the `Hierarchy` extension but have `has_many` relationships with other objects.
Let's say you have the following DataObject that has multiple levels of relationships, and an Admin section where the data of this object will be displayed.
You want the user to be able to view information regarding this object and all nested objects on the same page, without the need to navigate to each object individually.
In this case, you can use the following approach.

```php
class ParentNode extends DataObject
{
...
private static $has_many = [
'ChildNodes' => BranchNode::class,
];
}
```

```php
class ChildNode extends DataObject
{
...
private static $has_one = [
'ParentNode' => ParentNode::class,
];
private static $has_many = [
'GrandChildNodes' => GrandChildNode::class,
];
public function getNestedConfig(): GridFieldConfig
{
$config = new GridFieldConfig_RecordEditor();
$config->addComponent(GridFieldNestedForm::create()->setRelationName('GrandChildNodes'));
return $config;
}
}
```

```php
class GrandChildNode extends DataObject
{
...
private static $has_one = [
'ChildNode' => ChildNode::class,
];
}
```

```php
// Basic usage, defaults to the Children-method for Hierarchy objects.
$grid->getConfig()->addComponent(GridFieldNestedForm::create());
class MyAdminSection extends ModelAdmin
{
private static string $url_segment = 'my-admin-section';
private static string $menu_title = 'My Admin Section';
private static array $managed_models = [
RootNode::class
];
protected function getGridFieldConfig(): GridFieldConfig
{
$config = parent::getGridFieldConfig();
$config->addComponent(GridFieldNestedForm::create()->setRelationName('ChildNodes'));
return $config;
}
}
// Usage with custom relation
$grid->getConfig()->addComponent(GridFieldNestedForm::create()->setRelationName('MyRelation'));
```

There is also the possibility to use Nested GridField with the data structure ArrayList. To do this, you can use the following approach.

```php
class NonRelationalData extends DataObject
{
private static $table_name = 'NestedGridField_NonRelationalData';
private static $db = [
'Name' => 'Varchar(50)',
];
public function getList() {
$list = ArrayList::create();
$data = Company::get()->byIDs([1,2,3,4,5]);
foreach ($data as $value) {
$list->push($value);
}
return $list;
}
}
```

#### Additional features and settings

You can define your own custom GridField config for the nested GridField configuration by implementing a `getNestedConfig`
on your nested model (should return a `GridField_Config` object).
```php
Expand All @@ -185,7 +319,8 @@ class NestedObject extends DataObject
```

You can also modify the default config (a `GridFieldConfig_RecordEditor`) via an extension to the nested model class, by implementing
`updateNestedConfig`, which will get the config object as the first parameter.
`updateNestedConfig`, which will get the config object as the parameter.

```php
class NestedObjectExtension extends DataExtension
{
Expand All @@ -195,3 +330,10 @@ class NestedObjectExtension extends DataExtension
}
}
```

You can set the maximum number of nested levels using a `$default_max_nesting_level` configuration property. The default value is 10 levels.

```yml
Symbiote\GridFieldExtensions\GridFieldNestedForm;
default_max_nesting_level: 5
```

0 comments on commit 328dfe7

Please sign in to comment.