Skip to content

Commit

Permalink
Refactor to fail gracefully on GridFieldPaginator
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Nov 18, 2018
1 parent e8dc2f1 commit d990ecc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
34 changes: 21 additions & 13 deletions src/Forms/GridField/GridFieldDetailForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function handleItem($gridField, $request)
if (is_numeric($request->param('ID'))) {
/** @var Filterable $dataList */
$dataList = $gridField->getList();
$record = $dataList->byID($request->param("ID"));
$record = $dataList->byID($request->param('ID'));
} else {
$record = Injector::inst()->create($gridField->getModelClass());
}
Expand Down Expand Up @@ -199,22 +199,26 @@ public function getName()
/**
* @return bool
*/
private function getDefaultShowPagination()
protected function getDefaultShowPagination()
{
$formActionsConfig = GridFieldDetailForm_ItemRequest::config()->get("formActions");
return $formActionsConfig["showPagination"];
$formActionsConfig = GridFieldDetailForm_ItemRequest::config()->get('formActions');
return isset($formActionsConfig['showPagination']) ? (boolean) $formActionsConfig['showPagination'] : false;
}

/**
* @return bool
*/
public function getShowPagination()
{
return is_bool($this->showPagination) ? $this->showPagination : $this->getDefaultShowPagination();
if ($this->showPagination === null) {
return $this->getDefaultShowPagination();
}

return (boolean) $this->showPagination;
}

/**
* @param mixed $showPagination
* @param bool|null $showPagination
* @return GridFieldDetailForm
*/
public function setShowPagination($showPagination)
Expand All @@ -226,22 +230,26 @@ public function setShowPagination($showPagination)
/**
* @return bool
*/
private function getDefaultShowAdd()
protected function getDefaultShowAdd()
{
$formActionsConfig = GridFieldDetailForm_ItemRequest::config()->get("formActions");
return $formActionsConfig["showAdd"];
$formActionsConfig = GridFieldDetailForm_ItemRequest::config()->get('formActions');
return isset($formActionsConfig['showAdd']) ? (boolean) $formActionsConfig['showAdd'] : false;
}

/**
* @return bool
*/
public function getShowAdd()
{
return is_bool($this->showAdd) ? $this->showAdd : $this->getDefaultShowAdd();
if ($this->showAdd === null) {
return $this->getDefaultShowAdd();
}

return (boolean) $this->showAdd;
}

/**
* @param mixed $showAdd
* @param bool|null $showAdd
* @return GridFieldDetailForm
*/
public function setShowAdd($showAdd)
Expand Down Expand Up @@ -303,8 +311,8 @@ public function getItemRequestClass()
{
if ($this->itemRequestClass) {
return $this->itemRequestClass;
} elseif (ClassInfo::exists(static::class . "_ItemRequest")) {
return static::class . "_ItemRequest";
} elseif (ClassInfo::exists(static::class . '_ItemRequest')) {
return static::class . '_ItemRequest';
} else {
return GridFieldDetailForm_ItemRequest::class;
}
Expand Down
18 changes: 11 additions & 7 deletions src/Forms/GridField/GridFieldDetailForm_ItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public function doSave($data, $form)
*/
public function doPrevious($data, $form)
{
$this->getToplevelController()->getResponse()->addHeader("X-Pjax", "Content");
$this->getToplevelController()->getResponse()->addHeader('X-Pjax', 'Content');
$link = $this->getEditLink($this->getPreviousRecordID());
return $this->redirect($link);
}
Expand All @@ -480,7 +480,7 @@ public function doPrevious($data, $form)
*/
public function doNext($data, $form)
{
$this->getToplevelController()->getResponse()->addHeader("X-Pjax", "Content");
$this->getToplevelController()->getResponse()->addHeader('X-Pjax', 'Content');
$link = $this->getEditLink($this->getNextRecordID());
return $this->redirect($link);
}
Expand All @@ -495,7 +495,7 @@ public function doNext($data, $form)
*/
public function doNew($data, $form)
{
return $this->redirect(Controller::join_links($this->gridField->Link("item"), "new"));
return $this->redirect(Controller::join_links($this->gridField->Link('item'), 'new'));
}

/**
Expand All @@ -508,24 +508,28 @@ public function getEditLink($id)
{
return Controller::join_links(
$this->gridField->Link(),
"item",
'item',
$id,
'?gridState=' . urlencode($this->gridField->getState(false)->Value())
);
}

/**
* @param int $offset The offset from the current record
* @return bool
* @return int|bool
*/
private function getAdjacentRecordID($offset)
{
$gridField = $this->gridField;
$gridField = $this->getGridField();
$gridStateStr = $this->getRequest()->requestVar('gridState');
$state = $gridField->getState(false);
$state->setValue($gridStateStr);

$data = $state->getData();
$paginator = $data->getData('GridFieldPaginator');
if (!$paginator) {
return false;
}

$currentPage = $data->getData('GridFieldPaginator')->getData('currentPage');
$itemsPerPage = $data->getData('GridFieldPaginator')->getData('itemsPerPage');

Expand Down

0 comments on commit d990ecc

Please sign in to comment.