Skip to content

Commit

Permalink
ENH Restore gridfield state from get vars
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed May 22, 2022
1 parent 6c5448b commit a8652f7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Forms/GridField/GridField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use InvalidArgumentException;
use LogicException;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HasRequestHandler;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Control\NullHTTPRequest;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector;
Expand Down Expand Up @@ -45,6 +47,8 @@
*/
class GridField extends FormField
{
use GridFieldStateAware;

/**
* @var array
*/
Expand Down Expand Up @@ -435,6 +439,8 @@ private function initState(): void
{
$this->state = new GridState($this);

$this->addStateFromRequest();

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

foreach ($this->getComponents() as $item) {
Expand All @@ -444,6 +450,32 @@ private function initState(): void
}
}

/**
* Adds state for this gridfield from the request variables.
*
* If there is state already set on this GridField, that takes precedent
* over state from the request.
*/
private function addStateFromRequest(): void
{
$request = $this->getRequest();
if (($request instanceof NullHTTPRequest) && Controller::has_curr()) {
$request = Controller::curr()->getRequest();
}
if ($request->params()['Action']) {
return;
}
$stateStr = $this->getStateManager()->getStateFromRequest($this, $request);
if ($stateStr) {
$oldState = $this->getState(false);
// Create a dummy state so that we can merge the current state with the request state.
$newState = new GridState($this, $stateStr);
// Put the current state on top of the request state.
$newState->setValue($oldState->Value());
$this->state = $newState;
}
}

/**
* Returns the whole gridfield rendered with all the attached components.
*
Expand Down

0 comments on commit a8652f7

Please sign in to comment.