Skip to content

Commit

Permalink
NEW: Add GridField_StateProvider interface
Browse files Browse the repository at this point in the history
This interfaces provides a mechanism by which components can reliably
specify default state.
  • Loading branch information
Sam Minnee committed Aug 13, 2020
1 parent 3eaf4a3 commit 7bb657e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
20 changes: 18 additions & 2 deletions src/Forms/GridField/GridField.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ public function __construct($name, $title = null, SS_List $dataList = null, Grid

$this->setConfig($config);

$this->state = new GridState($this);

$this->addExtraClass('grid-field');
}

Expand Down Expand Up @@ -407,13 +405,31 @@ public function getManipulatedList()
*/
public function getState($getData = true)
{
// Initialise state on first call. This ensures it's evaluated after components have been added
if (!$this->state) {
$this->initState();
}

if ($getData) {
return $this->state->getData();
}

return $this->state;
}

private function initState()
{
$this->state = new GridState($this);

$data = $this->state->getData();

foreach ($this->getComponents() as $item) {
if ($item instanceof GridField_StateProvider) {
$item->initDefaultState($data);
}
}
}

/**
* Returns the whole gridfield rendered with all the attached components.
*
Expand Down
9 changes: 5 additions & 4 deletions src/Forms/GridField/GridFieldPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,15 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat
*/
protected function getGridPagerState(GridField $gridField)
{
$state = $gridField->State->GridFieldPaginator;
return $gridField->State->GridFieldPaginator;
}

$state->initDefaults([
public function initDefaultSate(GridState_Data $data)
{
$data->GridFieldPaginator->initDefaults([
'currentPage' => 1,
'itemsPerPage' => $this->getItemsPerPage()
]);

return $state;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Forms/GridField/GridFieldSortableHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @see GridField
*/
class GridFieldSortableHeader implements GridField_HTMLProvider, GridField_DataManipulator, GridField_ActionProvider
class GridFieldSortableHeader implements GridField_HTMLProvider, GridField_DataManipulator, GridField_ActionProvider, GridField_StateProvider
{

/**
Expand Down Expand Up @@ -281,9 +281,11 @@ public function getManipulatedData(GridField $gridField, SS_List $dataList)
*/
private function getState(GridField $gridField): GridState_Data
{
$state = $gridField->State->GridFieldSortableHeader;
$state->initDefaults(['SortColumn' => null, 'SortDirection' => 'asc']);
return $gridField->State->GridFieldSortableHeader;
}

return $state;
public function initDefaultState(GridState_Data $data): void
{
$data->initDefaults(['SortColumn' => null, 'SortDirection' => 'asc']);
}
}
21 changes: 21 additions & 0 deletions src/Forms/GridField/GridField_StateProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace SilverStripe\Forms\GridField;

/**
* A GridField component that provides state, notably default state.
*
* Implementation of this interface is optional; without it, no default state is assumed.
* The benefit of default state is that it won't be included in URLs, keeping URLs tidier.
*/
interface GridField_StateProvider extends GridFieldComponent
{
/**
* Initialise the default state in the given GridState_Data
*
* We recommend that you call $data->initDefaults() to do this.
*
* @param $data The top-level sate object
*/
public function initDefaultState(GridState_Data $data): void;
}

0 comments on commit 7bb657e

Please sign in to comment.