From a8652f7cbbc84470058a01732f59cbf5d66e68f0 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Mon, 23 May 2022 10:57:13 +1200 Subject: [PATCH] ENH Restore gridfield state from get vars --- src/Forms/GridField/GridField.php | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/Forms/GridField/GridField.php b/src/Forms/GridField/GridField.php index c26169456..6e5f621bf 100644 --- a/src/Forms/GridField/GridField.php +++ b/src/Forms/GridField/GridField.php @@ -4,10 +4,12 @@ namespace SilverStripe\Forms\GridField; 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; @@ -45,6 +47,8 @@ use SilverStripe\View\HTML; */ class GridField extends FormField { + use GridFieldStateAware; + /** * @var array */ @@ -435,6 +439,8 @@ class GridField extends FormField { $this->state = new GridState($this); + $this->addStateFromRequest(); + $data = $this->state->getData(); foreach ($this->getComponents() as $item) { @@ -444,6 +450,32 @@ class GridField extends FormField } } + /** + * 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. *