|
3 | 3 | namespace gorriecoe\Menu\Models; |
4 | 4 |
|
5 | 5 | use gorriecoe\Link\Models\Link; |
6 | | -use gorriecoe\Menu\Models\MenuSet; |
7 | 6 | use SilverStripe\Forms\FieldList; |
8 | 7 | use SilverStripe\Forms\GridField\GridField; |
9 | 8 | use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; |
10 | 9 | use SilverStripe\GraphQL\Scaffolding\Interfaces\ScaffoldingProvider; |
11 | 10 | use SilverStripe\GraphQL\Scaffolding\Scaffolders\SchemaScaffolder; |
12 | | -use SilverStripe\ORM\HasManyList; |
| 11 | +use SilverStripe\ORM\DataList; |
13 | 12 | use SilverStripe\ORM\DataObject; |
| 13 | +use SilverStripe\ORM\HasManyList; |
14 | 14 | use SilverStripe\Security\Member; |
15 | 15 | use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
16 | 16 |
|
@@ -256,4 +256,31 @@ public static function get_by_sitetreeID($menuSet, Int $siteTreeID) |
256 | 256 | 'SiteTreeID' => $siteTreeID |
257 | 257 | ]); |
258 | 258 | } |
| 259 | + |
| 260 | + public function onBeforeWrite() |
| 261 | + { |
| 262 | + parent::onBeforeWrite(); |
| 263 | + |
| 264 | + // When writing initial record, set position to last in menu |
| 265 | + if (!$this->isInDB() && is_null($this->Sort)) { |
| 266 | + $this->Sort = $this->getSiblings()->max('Sort') + 1; |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * Get sibling links |
| 272 | + * |
| 273 | + * @return DataList|MenuLink[] |
| 274 | + */ |
| 275 | + public function getSiblings(): DataList |
| 276 | + { |
| 277 | + $siblings = static::get(); |
| 278 | + if ($this->ParentID) { |
| 279 | + $siblings = $siblings->filter('ParentID', $this->ParentID); |
| 280 | + } |
| 281 | + if ($this->MenuSetID) { |
| 282 | + $siblings = $siblings->filter('MenuSetID', $this->MenuSetID); |
| 283 | + } |
| 284 | + return $siblings; |
| 285 | + } |
259 | 286 | } |
0 commit comments