Updating StateStore interface not to define a constructor & fixing GridFieldFilterHeader to add required attributes

This commit is contained in:
Guy Marriott 2018-11-22 13:05:43 +13:00
parent b4c8f699eb
commit ddaa22986f
No known key found for this signature in database
GPG Key ID: A80F9ACCB86D3DA7
7 changed files with 50 additions and 49 deletions

View File

@ -0,0 +1,34 @@
<?php
namespace SilverStripe\Forms\GridField\FormAction;
use SilverStripe\Control\HTTPRequest;
abstract class AbstractRequestAwareStore
{
private static $dependencies = [
'request' => '%$' . HTTPRequest::class,
];
/**
* @var HTTPRequest
*/
protected $request;
/**
* @return HTTPRequest
*/
public function getRequest()
{
return $this->request;
}
/**
* @param HTTPRequest $request
* @return $this
*/
public function setRequest($request)
{
$this->request = $request;
return $this;
}
}

View File

@ -6,21 +6,8 @@ use SilverStripe\Control\HTTPRequest;
/**
* Stores GridField action state on an attribute on the action and then analyses request parameters to load it back
*/
class AttributeStore implements StateStore
class AttributeStore extends AbstractRequestAwareStore implements StateStore
{
/**
* @var HTTPRequest
*/
protected $request;
/**
* @param HTTPRequest $request
*/
public function __construct(HTTPRequest $request)
{
$this->request = $request;
}
/**
* Save the given state against the given ID returning an associative array to be added as attributes on the form
* action
@ -41,11 +28,11 @@ class AttributeStore implements StateStore
* Load state for a given ID
*
* @param string $id
* @return mixed
* @return array
*/
public function load($id)
{
// Check the request
return json_decode($this->request->requestVar('ActionState'), true);
return (array) json_decode((string) $this->getRequest()->requestVar('ActionState'), true);
}
}

View File

@ -6,21 +6,8 @@ use SilverStripe\Control\HTTPRequest;
/**
* Stores GridField action state in the session in exactly the same way it has in the past
*/
class SessionStore implements StateStore
class SessionStore extends AbstractRequestAwareStore implements StateStore
{
/**
* @var HTTPRequest
*/
protected $request;
/**
* @param HTTPRequest $request
*/
public function __construct(HTTPRequest $request)
{
$this->request = $request;
}
/**
* Save the given state against the given ID returning an associative array to be added as attributes on the form
* action
@ -31,7 +18,7 @@ class SessionStore implements StateStore
*/
public function save($id, array $state)
{
$this->request->getSession()->set($id, $state);
$this->getRequest()->getSession()->set($id, $state);
// This adapter does not require any additional attributes...
return [];
@ -41,10 +28,10 @@ class SessionStore implements StateStore
* Load state for a given ID
*
* @param string $id
* @return mixed
* @return array
*/
public function load($id)
{
return $this->request->getSession()->get($id);
return (array) $this->getRequest()->getSession()->get($id);
}
}

View File

@ -1,15 +1,8 @@
<?php
namespace SilverStripe\Forms\GridField\FormAction;
use SilverStripe\Control\HTTPRequest;
interface StateStore
{
/**
* @param HTTPRequest $request
*/
public function __construct(HTTPRequest $request);
/**
* Save the given state against the given ID returning an associative array to be added as attributes on the form
* action
@ -24,7 +17,7 @@ interface StateStore
* Load state for a given ID
*
* @param string $id
* @return mixed
* @return array
*/
public function load($id);
}

View File

@ -1014,7 +1014,7 @@ class GridField extends FormField
// Fetch the store for the "state" of actions (not the GridField)
/** @var StateStore $store */
$store = Injector::inst()->create(StateStore::class . '.' . $this->getName(), $request);
$store = Injector::inst()->create(StateStore::class . '.' . $this->getName());
foreach ($data as $dataKey => $dataValue) {
if (preg_match('/^action_gridFieldAlterAction\?StateID=(.*)/', $dataKey, $matches)) {

View File

@ -289,14 +289,18 @@ class GridFieldFilterHeader implements GridField_URLHandler, GridField_HTMLProvi
}, array_keys($filters)), $filters);
}
$searchAction = GridField_FormAction::create($gridField, 'filter', false, 'filter', null);
$clearAction = GridField_FormAction::create($gridField, 'reset', false, 'reset', null);
$schema = [
'formSchemaUrl' => $schemaUrl,
'name' => $searchField,
'placeholder' => _t(__CLASS__ . '.Search', 'Search "{name}"', ['name' => $name]),
'filters' => $filters ?: new \stdClass, // stdClass maps to empty json object '{}'
'gridfield' => $gridField->getName(),
'searchAction' => GridField_FormAction::create($gridField, 'filter', false, 'filter', null)->getAttribute('name'),
'clearAction' => GridField_FormAction::create($gridField, 'reset', false, 'reset', null)->getAttribute('name')
'searchAction' => $searchAction->getAttribute('name'),
'searchActionState' => $searchAction->getAttribute('data-action-state'),
'clearAction' => $clearAction->getAttribute('name'),
'clearActionState' => $clearAction->getAttribute('data-action-state'),
];
return Convert::raw2json($schema);

View File

@ -108,11 +108,7 @@ class GridField_FormAction extends FormAction
// Create a "store" for the "state" of this action
/** @var StateStore $store */
$store = Injector::inst()->create(
StateStore::class . '.' . $this->gridField->getName(),
// For some reason `getRequest` on GridField_FormAction does not return the correct request
Controller::curr()->getRequest()
);
$store = Injector::inst()->create(StateStore::class . '.' . $this->gridField->getName());
// Store the state and update attributes as required
$attributes += $store->save($key, $state);