Skip to content

Commit

Permalink
ENH Keep Request in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Jul 20, 2022
1 parent a8652f7 commit 93ff035
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
41 changes: 38 additions & 3 deletions src/Forms/GridField/GridField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use LogicException;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HasRequestHandler;
use SilverStripe\Control\HTTP;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
Expand Down Expand Up @@ -462,10 +463,9 @@ private function addStateFromRequest(): void
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.
Expand All @@ -476,6 +476,41 @@ private function addStateFromRequest(): void
}
}

/**
* Add get and post parameters if exist to the URL.
* URL keeps state for GridField, that exist in few different tabs.
*/
public function addRequestToURL(string $link): string
{
$request = $this->getRequest();

if (($request instanceof NullHTTPRequest) && Controller::has_curr()) {
$request = Controller::curr()->getRequest();
}

$postparams = [];

if ($request->postVars()) {
foreach ($request->postVars() as $key => $val) {
if (stripos($key, 'gridState') === 0) {
$postparams[$key] = $val;
}
}
}

$params = array_merge($request->getVars(), $postparams);

if ($params && is_array($params) && count($params) > 0) {
foreach ($params as $key => $val) {
$link = HTTP::setGetVar($key, $val, $link);
}
}

$this->extend('updateRequestToURL', $link);

return $link;
}

/**
* Returns the whole gridfield rendered with all the attached components.
*
Expand Down
12 changes: 11 additions & 1 deletion src/Forms/GridField/GridFieldDetailForm_ItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ public function getEditLink($id)
$id
);

$link = $this->gridField->addRequestToURL($link);

return $this->getStateManager()->addStateToURL($this->gridField, $link);
}

Expand Down Expand Up @@ -804,11 +806,19 @@ public function Breadcrumbs($unlinked = false)
$items = new ArrayList();
}

$params = $this->getRequest()->getVars();
if (isset($params['url'])) {
unset($params['url']);
}

if ($this->record && $this->record->ID) {
$title = ($this->record->Title) ? $this->record->Title : "#{$this->record->ID}";
$items->push(new ArrayData([
'Title' => $title,
'Link' => $this->Link()
'Link' => Controller::join_links(
$this->Link(),
'?' . http_build_query($params ?? [])
),
]));
} else {
$items->push(new ArrayData([
Expand Down
2 changes: 2 additions & 0 deletions src/Forms/GridField/GridFieldEditButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public function getUrl($gridField, $record, $columnName, $addState = true)
'edit'
);

$link = $gridField->addRequestToURL($link);

if ($addState) {
$link = $this->getStateManager()->addStateToURL($gridField, $link);
}
Expand Down
33 changes: 33 additions & 0 deletions tests/php/Forms/GridField/GridFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use InvalidArgumentException;
use LogicException;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\CSSContentParser;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
Expand Down Expand Up @@ -659,4 +661,35 @@ public function testValidationMessageInOutput()
$gridfieldOutput = $gridField->FieldHolder();
$this->assertStringNotContainsString('<p class="message ' . ValidationResult::TYPE_ERROR . '">', $gridfieldOutput);
}

public function testAddRequestToURL()
{
$config = new GridFieldConfig();
$data = new ArrayList([1, 2, 3, 4, 5, 6]);
$gridField = new GridField('testfield', 'testfield', $data, $config);
$link = Controller::join_links('class-name', 'item', '1');

$request = new HTTPRequest(
'POST',
'admin/gridfield',
[
'query' => 'postQuery',
'field' => '1'
]
);

$gridField->setRequest($request);
$this->assertEquals($gridField->addRequestToURL($link), "/class-name/item/1?query=postQuery&field=1");

$request = new HTTPRequest(
'GET',
'admin/gridfield',
[
'query' => 'getQuery',
'field' => '2'
]
);
$gridField->setRequest($request);
$this->assertEquals($gridField->addRequestToURL($link), "/class-name/item/1?query=getQuery&field=2");
}
}

0 comments on commit 93ff035

Please sign in to comment.