Skip to content

Commit 648a94b

Browse files
authored
Merge pull request #9 from tractorcow/fix/menu-sort
ENHANCEMENT Ensure that new menu links appear at the bottom of the sort list
2 parents 42b63da + ad0901f commit 648a94b

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/models/MenuLink.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace gorriecoe\Menu\Models;
44

55
use gorriecoe\Link\Models\Link;
6-
use gorriecoe\Menu\Models\MenuSet;
76
use SilverStripe\Forms\FieldList;
87
use SilverStripe\Forms\GridField\GridField;
98
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
109
use SilverStripe\GraphQL\Scaffolding\Interfaces\ScaffoldingProvider;
1110
use SilverStripe\GraphQL\Scaffolding\Scaffolders\SchemaScaffolder;
12-
use SilverStripe\ORM\HasManyList;
11+
use SilverStripe\ORM\DataList;
1312
use SilverStripe\ORM\DataObject;
13+
use SilverStripe\ORM\HasManyList;
1414
use SilverStripe\Security\Member;
1515
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
1616

@@ -256,4 +256,31 @@ public static function get_by_sitetreeID($menuSet, Int $siteTreeID)
256256
'SiteTreeID' => $siteTreeID
257257
]);
258258
}
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+
}
259286
}

0 commit comments

Comments
 (0)