Skip to content

Commit

Permalink
API Replace FormActions with anchors to enable panel-based loading in…
Browse files Browse the repository at this point in the history
… GridField navigation buttons
  • Loading branch information
robbieaverill committed May 1, 2019
1 parent a6c32e9 commit aa649dc
Showing 1 changed file with 42 additions and 63 deletions.
105 changes: 42 additions & 63 deletions src/Forms/GridField/GridFieldDetailForm_ItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
Expand All @@ -23,6 +22,7 @@
use SilverStripe\ORM\ValidationException;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\View\ArrayData;
use SilverStripe\View\HTML;
use SilverStripe\View\SSViewer;

class GridFieldDetailForm_ItemRequest extends RequestHandler
Expand Down Expand Up @@ -281,40 +281,58 @@ protected function getRightGroupField()
$rightGroup->setFieldHolderTemplate(get_class($rightGroup) . '_holder_buttongroup');

$previousAndNextGroup = CompositeField::create()->setName('PreviousAndNextGroup');
$previousAndNextGroup->addExtraClass('circular-group mr-2');
$previousAndNextGroup->setFieldHolderTemplate(get_class($previousAndNextGroup) . '_holder_buttongroup');
$previousAndNextGroup->addExtraClass('btn-group--circular mr-2');
$previousAndNextGroup->setFieldHolderTemplate(CompositeField::class . '_holder_buttongroup');

/** @var GridFieldDetailForm $component */
$component = $this->gridField->getConfig()->getComponentByType(GridFieldDetailForm::class);
$paginator = $this->getGridField()->getConfig()->getComponentByType(GridFieldPaginator::class);
$gridState = $this->getRequest()->requestVar('gridState');
if ($component && $paginator && $component->getShowPagination()) {
$previousAndNextGroup->push(FormAction::create('doPrevious')
->setUseButtonTag(true)
->setAttribute('data-grid-state', $gridState)
->setDisabled(!$this->getPreviousRecordID())
->setDescription(_t(__CLASS__ . '.PREVIOUS', 'Go to previous record'))
->setAttribute('aria-label', _t(__CLASS__ . '.PREVIOUS', 'Go to previous record'))
->addExtraClass('btn btn-secondary font-icon-left-open action--previous discard-confirmation'));
$previousAndNextGroup->push(
LiteralField::create(
'previous-record',
HTML::createTag('a', [
'href' => $this->getEditLink($this->getPreviousRecordID()),
'data-grid-state' => $gridState,
'disabled' => !$this->getPreviousRecordID(),
'title' => _t(__CLASS__ . '.PREVIOUS', 'Go to previous record'),
'aria-label' => _t(__CLASS__ . '.PREVIOUS', 'Go to previous record'),
'class' => 'btn btn-secondary font-icon-left-open action--previous discard-confirmation',
])
)
);

$previousAndNextGroup->push(FormAction::create('doNext')
->setUseButtonTag(true)
->setAttribute('data-grid-state', $gridState)
->setDisabled(!$this->getNextRecordID())
->setDescription(_t(__CLASS__ . '.NEXT', 'Go to next record'))
->setAttribute('aria-label', _t(__CLASS__ . '.NEXT', 'Go to next record'))
->addExtraClass('btn btn-secondary font-icon-right-open action--next discard-confirmation'));
$previousAndNextGroup->push(
LiteralField::create(
'next-record',
HTML::createTag('a', [
'href' => $this->getEditLink($this->getNextRecordID()),
'data-grid-state' => $gridState,
'disabled' => !$this->getNextRecordID(),
'title' => _t(__CLASS__ . '.NEXT', 'Go to next record'),
'aria-label' => _t(__CLASS__ . '.NEXT', 'Go to next record'),
'class' => 'btn btn-secondary font-icon-right-open action--next discard-confirmation',
])
)
);
}

$rightGroup->push($previousAndNextGroup);

if ($component && $component->getShowAdd()) {
$rightGroup->push(FormAction::create('doNew')
->setUseButtonTag(true)
->setAttribute('data-grid-state', $this->getRequest()->getVar('gridState'))
->setDescription(_t(__CLASS__ . '.NEW', 'Add new record'))
->setAttribute('aria-label', _t(__CLASS__ . '.NEW', 'Add new record'))
->addExtraClass('btn btn-primary font-icon-plus-thin circular action--new discard-confirmation'));
$rightGroup->push(
LiteralField::create(
'new-record',
HTML::createTag('a', [
'href' => Controller::join_links($this->gridField->Link('item'), 'new'),
'data-grid-state' => $gridState,
'title' => _t(__CLASS__ . '.NEW', 'Add new record'),
'aria-label' => _t(__CLASS__ . '.NEW', 'Add new record'),
'class' => 'btn btn-primary font-icon-plus-thin btn--circular action--new discard-confirmation',
])
)
);
}

return $rightGroup;
Expand All @@ -327,7 +345,7 @@ protected function getRightGroupField()
*/
protected function getFormActions()
{
$actions = new FieldList();
$actions = FieldList::create();

if ($this->record->ID !== 0) { // existing record
if ($this->record->canEdit()) {
Expand Down Expand Up @@ -469,45 +487,6 @@ public function doSave($data, $form)
return $this->redirectAfterSave($isNewRecord);
}

/**
* Goes to the previous record
* @param array $data The form data
* @param Form $form The Form object
* @return HTTPResponse
*/
public function doPrevious($data, $form)
{
$this->getToplevelController()->getResponse()->addHeader('X-Pjax', 'Content');
$link = $this->getEditLink($this->getPreviousRecordID());
return $this->redirect($link);
}

/**
* Goes to the next record
* @param array $data The form data
* @param Form $form The Form object
* @return HTTPResponse
*/
public function doNext($data, $form)
{
$this->getToplevelController()->getResponse()->addHeader('X-Pjax', 'Content');
$link = $this->getEditLink($this->getNextRecordID());
return $this->redirect($link);
}

/**
* Creates a new record. If you're already creating a new record,
* this forces the URL to change.
*
* @param array $data The form data
* @param Form $form The Form object
* @return HTTPResponse
*/
public function doNew($data, $form)
{
return $this->redirect(Controller::join_links($this->gridField->Link('item'), 'new'));
}

/**
* Gets the edit link for a record
*
Expand Down

0 comments on commit aa649dc

Please sign in to comment.