|
4 | 4 |
|
5 | 5 | use Psr\Cache\InvalidArgumentException; |
6 | 6 | use SilverStripe\Forms\GridField\GridField; |
| 7 | +use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
| 8 | +use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
7 | 9 | use SilverStripe\Forms\GridField\GridField_DataManipulator; |
8 | 10 | use SilverStripe\Forms\GridField\GridField_HTMLProvider; |
9 | 11 | use SilverStripe\ORM\SS_List; |
@@ -54,6 +56,15 @@ public function setMaxItems($maxItems) { |
54 | 56 | $this->maxItems = (int) $maxItems; |
55 | 57 | } |
56 | 58 |
|
| 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 | + |
57 | 68 | /** |
58 | 69 | * Indicates that the 'Add New [x]' button should be removed once we reach our limit. |
59 | 70 | * |
@@ -95,6 +106,23 @@ public function setRemoveFromTop($removeFromTop) { |
95 | 106 | return $this; |
96 | 107 | } |
97 | 108 |
|
| 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 | + |
98 | 126 | /** |
99 | 127 | * Allows you to perform some sort of action BEFORE any sort of manipulation is performed. |
100 | 128 | * |
@@ -153,7 +181,7 @@ public function getManipulatedData(GridField $gridField, SS_List $dataList) { |
153 | 181 | if(($dataList instanceof UnsavedRelationList)) return $dataList; |
154 | 182 |
|
155 | 183 | // Not compatible with paginator. |
156 | | - if ($gridField->getConfig()->getComponentByType('GridFieldPaginator')) { |
| 184 | + if ($gridField->getConfig()->getComponentByType(GridFieldPaginator::class)) { |
157 | 185 | $this->debug('GridFieldLimitItems is not compatible with GridFieldPaginator.'); |
158 | 186 | return $dataList; |
159 | 187 | } |
@@ -188,7 +216,7 @@ public function getManipulatedData(GridField $gridField, SS_List $dataList) { |
188 | 216 | if ($this->removeButton && $total >= $this->maxItems) { |
189 | 217 | // ... obviously shouldn't be null, but just in case. |
190 | 218 | $gridConfig = $gridField->getConfig(); |
191 | | - if ($gridConfig) $gridConfig->removeComponentsByType('GridFieldAddNewButton'); |
| 219 | + if ($gridConfig) $gridConfig->removeComponentsByType(GridFieldAddNewButton::class); |
192 | 220 | } |
193 | 221 |
|
194 | 222 | // Allow custom action after manipulation. |
|
0 commit comments