|
| 1 | +--- |
| 2 | +title: Configuration |
| 3 | +summary: Configuration options including adding existing search, inline editing, multi-class adding, orderable rows, and configurable pagination |
| 4 | +icon: cogs |
| 5 | +--- |
| 6 | + |
| 7 | +# Configuration |
| 8 | + |
| 9 | +## Add existing search |
| 10 | + |
| 11 | +The [`GridFieldAddExistingSearchButton`](api:Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton) component provides a more complete solution for adding |
| 12 | +existing records than a basic autocomplete. It uses the search context constructed by the model |
| 13 | +class to provide the search form. |
| 14 | + |
| 15 | +```php |
| 16 | +use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
| 17 | + |
| 18 | +$gridField->getConfig()->addComponent(GridFieldAddExistingSearchButton::create()); |
| 19 | +``` |
| 20 | + |
| 21 | +## Inline editing |
| 22 | + |
| 23 | +This example replaces the default data columns component with an inline editable one, and the |
| 24 | +default add new button with one that adds new records inline. |
| 25 | + |
| 26 | +```php |
| 27 | +use SilverStripe\Forms\GridField\GridField; |
| 28 | +use SilverStripe\Forms\GridField\GridFieldButtonRow; |
| 29 | +use SilverStripe\Forms\GridField\GridFieldConfig; |
| 30 | +use SilverStripe\Forms\GridField\GridFieldDeleteAction; |
| 31 | +use SilverStripe\Forms\GridField\GridFieldToolbarHeader; |
| 32 | +use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton; |
| 33 | +use Symbiote\GridFieldExtensions\GridFieldEditableColumns; |
| 34 | +use Symbiote\GridFieldExtensions\GridFieldTitleHeader; |
| 35 | + |
| 36 | +$gridField = GridField::create( |
| 37 | + 'ExampleGrid', |
| 38 | + 'Example GridField', |
| 39 | + $this->Items(), |
| 40 | + GridFieldConfig::create() |
| 41 | + ->addComponent(GridFieldButtonRow::create('before')) |
| 42 | + ->addComponent(GridFieldToolbarHeader::create()) |
| 43 | + ->addComponent(GridFieldTitleHeader::create()) |
| 44 | + ->addComponent(GridFieldEditableColumns::create()) |
| 45 | + ->addComponent(GridFieldDeleteAction::create()) |
| 46 | + ->addComponent(GridFieldAddNewInlineButton::create()) |
| 47 | +); |
| 48 | +``` |
| 49 | + |
| 50 | +You can customise the form fields that are used in the GridField by calling `setDisplayFields()` on the |
| 51 | +inline editing component. By default field scaffolding will be used. |
| 52 | + |
| 53 | +```php |
| 54 | +use SilverStripe\Forms\ReadonlyField; |
| 55 | +use SilverStripe\Forms\TextField; |
| 56 | +use Symbiote\GridFieldExtensions\GridFieldEditableColumns; |
| 57 | + |
| 58 | +$gridField->getConfig()->getComponentByType(GridFieldEditableColumns::class)->setDisplayFields([ |
| 59 | + 'FirstField' => function ($record, $column, $grid) { |
| 60 | + return TextField::create($column); |
| 61 | + }, |
| 62 | + 'SecondField' => [ |
| 63 | + 'title' => 'Custom Title', |
| 64 | + 'field' => ReadonlyField::class, |
| 65 | + ], |
| 66 | + 'ThirdField' => [ |
| 67 | + 'title' => 'Custom Title Two', |
| 68 | + 'callback' => function ($record, $column, $grid) { |
| 69 | + return TextField::create($column); |
| 70 | + }, |
| 71 | + ], |
| 72 | +]); |
| 73 | +``` |
| 74 | + |
| 75 | +Editing data contained in `many_many_extraFields` is supported - just treat it as you would any other field. |
| 76 | + |
| 77 | +## Multi class adding |
| 78 | + |
| 79 | +The [`GridFieldAddNewMultiClass`](api:Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass) allows the user to select the record type to create when creating |
| 80 | +a new record. By default it allows them to select the model class for the GridField, or any |
| 81 | +subclasses. You can control the createable classes using the `setClasses()` method. |
| 82 | + |
| 83 | +```php |
| 84 | +use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
| 85 | +use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass; |
| 86 | + |
| 87 | +$gridField->getConfig() |
| 88 | + ->removeComponentsByType(GridFieldAddNewButton::class) |
| 89 | + ->addComponent(GridFieldAddNewMultiClass::create()); |
| 90 | +``` |
| 91 | + |
| 92 | +## Orderable rows |
| 93 | + |
| 94 | +The [`GridFieldOrderableRows`](api:Symbiote\GridFieldExtensions\GridFieldOrderableRows) component allows drag-and-drop reordering of any list type. The field |
| 95 | +used to store the sort is set by passing a constructor parameter to the component, or calling |
| 96 | +`setSortField()`. For `many_many` relationships, the sort field should normally be an extra field on |
| 97 | +the relationship. |
| 98 | + |
| 99 | +```php |
| 100 | +use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
| 101 | + |
| 102 | +// Basic usage, defaults to "Sort" for the sort field. |
| 103 | +$gridField->getConfig()->addComponent(GridFieldOrderableRows::create()); |
| 104 | + |
| 105 | +// Specifying the sort field. |
| 106 | +$gridField->getConfig()->addComponent(GridFieldOrderableRows::create('SortField')); |
| 107 | +``` |
| 108 | + |
| 109 | +By default, when you create a new item, it is created with a sort order of "0" - that is, it is added |
| 110 | +to the start of the list. The sort order is only set for the first time when the user reorders the items. |
| 111 | +If you wish to append newly created items to the end of the list, use an `onBeforeWrite()` hook like: |
| 112 | + |
| 113 | +```php |
| 114 | +namespace App\Models; |
| 115 | + |
| 116 | +use SilverStripe\ORM\DataObject; |
| 117 | + |
| 118 | +class Item extends DataObject |
| 119 | +{ |
| 120 | + // ... |
| 121 | + private static $db = [ |
| 122 | + 'Sort' => 'Int', |
| 123 | + ]; |
| 124 | + |
| 125 | + protected function onBeforeWrite() |
| 126 | + { |
| 127 | + if (!$this->Sort) { |
| 128 | + $this->Sort = Item::get()->max('Sort') + 1; |
| 129 | + } |
| 130 | + parent::onBeforeWrite(); |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +### Versioning |
| 136 | + |
| 137 | +By default `GridFieldOrderableRows` will handle versioning but won't automatically publish any records. The user will need to go into each record and publish them manually which could get cumbersome for large lists. |
| 138 | + |
| 139 | +You can configure the list to automatically publish a record if the record is the latest version and is already published. This won't publish any records which have draft changes. |
| 140 | + |
| 141 | +```php |
| 142 | +use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
| 143 | + |
| 144 | +$orderable = GridFieldOrderableRows::create()->setRepublishLiveRecords(true); |
| 145 | +``` |
| 146 | + |
| 147 | +There are caveats with both approaches so consideration should be made for which approach best suits the requirements. |
| 148 | + |
| 149 | +> [!NOTE] |
| 150 | +> There is a limitation when using `GridFieldOrderableRows` on unsaved data objects; namely, that it doesn't work as without data being saved, the list of related objects has no context. Please check `$this->ID` before adding the `GridFieldOrderableRows` component to the GridField config (or even, before adding the GridField at all). |
| 151 | +
|
| 152 | +## Configurable paginator |
| 153 | + |
| 154 | +The [`GridFieldConfigurablePaginator`](api:Symbiote\GridFieldExtensions\GridFieldConfigurablePaginator) component allows you to have a page size dropdown added to your GridField |
| 155 | +pagination controls. The page sizes are configurable via the configuration system, or at call time using the public API. |
| 156 | +To use this component you should remove the original paginator component first: |
| 157 | + |
| 158 | +```php |
| 159 | +use Symbiote\GridFieldExtensions\GridFieldConfigurablePaginator; |
| 160 | + |
| 161 | +$gridField->getConfig() |
| 162 | + ->removeComponentsByType('GridFieldPaginator') |
| 163 | + ->addComponent(GridFieldConfigurablePaginator::create()); |
| 164 | +``` |
| 165 | + |
| 166 | +You can configure the page sizes with the configuration system. Note that merging is the default strategy, so to replace |
| 167 | +the default sizes with your own you will need to unset the original first, for example: |
| 168 | + |
| 169 | +```php |
| 170 | +// app/_config.php |
| 171 | +use SilverStripe\Core\Config\Config; |
| 172 | + |
| 173 | +Config::inst()->remove('GridFieldConfigurablePaginator', 'default_page_sizes'); |
| 174 | +Config::inst()->update('GridFieldConfigurablePaginator', 'default_page_sizes', [100, 200, 500]); |
| 175 | +``` |
| 176 | + |
| 177 | +You can also override these at call time: |
| 178 | + |
| 179 | +```php |
| 180 | +use Symbiote\GridFieldExtensions\GridFieldConfigurablePaginator; |
| 181 | + |
| 182 | +$paginator = GridFieldConfigurablePaginator::create(100, [100, 200, 500]); |
| 183 | +$paginator->setPageSizes([200, 500, 1000]); |
| 184 | +$paginator->setItemsPerPage(500); |
| 185 | +``` |
0 commit comments