Skip to content

Commit ee65708

Browse files
committed
upgrade/ss4-with-enforced-limit: added code snippets from separate pull request.
1 parent 5f7a8aa commit ee65708

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

code/GridFieldConfig_LimitedRelationEditor.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
* @since 2016-02-24
1212
*/
1313
class GridFieldConfig_LimitedRelationEditor extends GridFieldConfig_RelationEditor {
14+
/**
15+
* used instance of the GridFieldLimitItems class.
16+
*
17+
* @var GridFieldLimitItems
18+
*/
19+
protected $limiter;
1420

1521
/**
1622
* Setup new configuration for a relation with a hard maximum limit of items.
@@ -25,7 +31,8 @@ public function __construct($maxItems) {
2531
$this->removeComponentsByType('GridFieldPageCount');
2632

2733
// Setup GridFieldLimitItems.
28-
$this->addComponent(new GridFieldLimitItems($maxItems));
34+
$this->limiter = new GridFieldLimitItems($maxItems);
35+
$this->addComponent($this->limiter);
2936
}
3037

3138
/**
@@ -39,4 +46,16 @@ public function __construct($maxItems) {
3946
public function addComponent(GridFieldComponent $component, $insertBefore = 'GridFieldLimitItems') {
4047
return parent::addComponent($component, $insertBefore);
4148
}
49+
50+
/**
51+
* allows you to enforce the set limit by removing options to add new items
52+
*
53+
* @return GridFieldConfig_LimitedRelationEditor
54+
*/
55+
public function enforceLimit()
56+
{
57+
// set the limit to be enforced
58+
$this->limiter->enforceLimit();
59+
return $this;
60+
}
4261
}

code/GridFieldLimitItems.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Psr\Cache\InvalidArgumentException;
66
use SilverStripe\Forms\GridField\GridField;
7+
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
8+
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
79
use SilverStripe\Forms\GridField\GridField_DataManipulator;
810
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
911
use SilverStripe\ORM\SS_List;
@@ -54,6 +56,15 @@ public function setMaxItems($maxItems) {
5456
$this->maxItems = (int) $maxItems;
5557
}
5658

59+
/**
60+
* The maximum number of items you wish to allow in this grid field.
61+
*
62+
* @return int $maxItems
63+
*/
64+
public function getMaxItems() {
65+
return $this->maxItems;
66+
}
67+
5768
/**
5869
* Indicates that the 'Add New [x]' button should be removed once we reach our limit.
5970
*
@@ -95,6 +106,23 @@ public function setRemoveFromTop($removeFromTop) {
95106
return $this;
96107
}
97108

109+
/**
110+
* allows you to enforce the set limit by removing options to add new items
111+
*/
112+
public function enforceLimit()
113+
{
114+
// ensure we are removing the buttons if these are
115+
$this->onAfterManipulate(function(GridField $grid, SS_List $list){
116+
if ($list->count() == $this->getMaxItems()) {
117+
// var_dump($grid->getConfig());
118+
// die();
119+
120+
$grid->getConfig()->removeComponentsByType(GridFieldAddNewButton::class);
121+
$grid->getConfig()->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
122+
}
123+
});
124+
}
125+
98126
/**
99127
* Allows you to perform some sort of action BEFORE any sort of manipulation is performed.
100128
*
@@ -153,7 +181,7 @@ public function getManipulatedData(GridField $gridField, SS_List $dataList) {
153181
if(($dataList instanceof UnsavedRelationList)) return $dataList;
154182

155183
// Not compatible with paginator.
156-
if ($gridField->getConfig()->getComponentByType('GridFieldPaginator')) {
184+
if ($gridField->getConfig()->getComponentByType(GridFieldPaginator::class)) {
157185
$this->debug('GridFieldLimitItems is not compatible with GridFieldPaginator.');
158186
return $dataList;
159187
}
@@ -188,7 +216,7 @@ public function getManipulatedData(GridField $gridField, SS_List $dataList) {
188216
if ($this->removeButton && $total >= $this->maxItems) {
189217
// ... obviously shouldn't be null, but just in case.
190218
$gridConfig = $gridField->getConfig();
191-
if ($gridConfig) $gridConfig->removeComponentsByType('GridFieldAddNewButton');
219+
if ($gridConfig) $gridConfig->removeComponentsByType(GridFieldAddNewButton::class);
192220
}
193221

194222
// Allow custom action after manipulation.

0 commit comments

Comments
 (0)