Skip to content

Commit

Permalink
MINOR: Add ability to customise the visibility of the Previous, `Ne…
Browse files Browse the repository at this point in the history
…xt` and `Add` buttons at a `GridField` level
  • Loading branch information
bergice committed Nov 12, 2018
1 parent 14e2cc2 commit 2ae244b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ SilverStripe\Admin\LeftAndMain:
## Customising the CMS form actions
The `Previous`, `Next` and `Add` actions on the edit form are visible by default but can be hidden by adding the following `.yml` config:
The `Previous`, `Next` and `Add` actions on the edit form are visible by default but can be hidden globally by adding the following `.yml` config:

```yml
FormActions:
Expand All @@ -135,6 +135,16 @@ FormActions:
showAdd: false
```

You can also configure this for a specific `GridField` instance when using the `GridFieldConfig_RecordEditor` constructor:

```php
$grid = new GridField(
"pages",
"All Pages",
SiteTree::get(),
GridFieldConfig_RecordEditor::create(null, false, false, false));
```

## Related

* [How to extend the CMS interface](extend_cms_interface)
7 changes: 5 additions & 2 deletions src/Forms/GridField/GridFieldConfig_RecordEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ class GridFieldConfig_RecordEditor extends GridFieldConfig
/**
*
* @param int $itemsPerPage - How many items per page should show up
* @param bool $showPrevious Whether the `Previous` button should display or not, leave as null to use default
* @param bool $showNext Whether the `Next` button should display or not, leave as null to use default
* @param bool $showAdd Whether the `Add` button should display or not, leave as null to use default
*/
public function __construct($itemsPerPage = null)
public function __construct($itemsPerPage = null, $showPrevious = null, $showNext = null, $showAdd = null)
{
parent::__construct();

Expand All @@ -26,7 +29,7 @@ public function __construct($itemsPerPage = null)
$this->addComponent(new GridField_ActionMenu());
$this->addComponent(new GridFieldPageCount('toolbar-header-right'));
$this->addComponent($pagination = new GridFieldPaginator($itemsPerPage));
$this->addComponent(new GridFieldDetailForm());
$this->addComponent(new GridFieldDetailForm(null, $showPrevious, $showNext, $showAdd));

$sort->setThrowExceptionOnBadDataType(false);
$filter->setThrowExceptionOnBadDataType(false);
Expand Down
52 changes: 50 additions & 2 deletions src/Forms/GridField/GridFieldDetailForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ class GridFieldDetailForm implements GridField_URLHandler
*/
protected $name;

/**
*
* @var bool
*/
protected $showPrevious;

/**
*
* @var bool
*/
protected $showNext;

/**
*
* @var bool
*/
protected $showAdd;

/**
* @var Validator The form validator used for both add and edit fields.
*/
Expand Down Expand Up @@ -79,10 +97,16 @@ public function getURLHandlers($gridField)
* controller who wants to display the getCMSFields
*
* @param string $name The name of the edit form to place into the pop-up form
* @param bool $showPrevious Whether the `Previous` button should display or not, leave as null to use default
* @param bool $showNext Whether the `Next` button should display or not, leave as null to use default
* @param bool $showAdd Whether the `Add` button should display or not, leave as null to use default
*/
public function __construct($name = 'DetailForm')
public function __construct($name = null, $showPrevious = null, $showNext = null, $showAdd = null)
{
$this->name = $name;
$this->name = $name ?: 'DetailForm';
$this->showPrevious = $showPrevious;
$this->showNext = $showNext;
$this->showAdd = $showAdd;
}

/**
Expand Down Expand Up @@ -183,6 +207,30 @@ public function getName()
return $this->name;
}

/**
* @return bool
*/
public function getShowPrevious()
{
return $this->showPrevious;
}

/**
* @return bool
*/
public function getShowNext()
{
return $this->showNext;
}

/**
* @return bool
*/
public function getShowAdd()
{
return $this->showAdd;
}

/**
* @param Validator $validator
* @return $this
Expand Down
10 changes: 6 additions & 4 deletions src/Forms/GridField/GridFieldDetailForm_ItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,13 @@ protected function getFormActions()
$previousAndNextGroup->addExtraClass('rounded');
$previousAndNextGroup->setFieldHolderTemplate(get_class($previousAndNextGroup) . '_holder_buttongroup');


$component = $this->gridField->getConfig()->getComponentByType(GridFieldDetailForm::class);
$formActionsConfig = $this->config()->get("formActions");
$showPrevious = $formActionsConfig["showPrevious"];
$showNext = $formActionsConfig["showNext"];
$showAdd = $formActionsConfig["showAdd"];

$showPrevious = is_bool($component->getShowPrevious()) ? $component->getShowPrevious() : $formActionsConfig["showPrevious"];
$showNext = is_bool($component->getShowNext()) ? $component->getShowNext() : $formActionsConfig["showNext"];
$showAdd = is_bool($component->getShowAdd()) ? $component->getShowAdd() : $formActionsConfig["showAdd"];

if ($showPrevious) {
$previousAndNextGroup->push(FormAction::create('doPrevious')
Expand Down Expand Up @@ -515,7 +518,6 @@ public function getEditLink($id)
$this->gridField->Link(),
"item",
$id,
// todo: use http header instead
'?gridState=' . urlencode($this->gridField->getState(false)->Value())
);
}
Expand Down

0 comments on commit 2ae244b

Please sign in to comment.