public function onBeforeWrite()
    {
        parent::onBeforeWrite();

        if (!$this->Sort) {
            if ($this->hasExtension(Versioned::class)) {
                $records = Versioned::get_by_stage(BaseElement::class, Versioned::DRAFT);
            } else {
                $records = BaseElement::get();
            }

            $records = $records->filter('ParentID', $this->ParentID);

            $this->Sort = $records->max('Sort') + 1;
        }
    }
public function onBeforeWrite()
    {
        parent::onBeforeWrite();

        if (!$this->Sort) {
            if ($this->hasExtension(Versioned::class)) {
                $records = Versioned::get_by_stage(BaseElement::class, Versioned::DRAFT);
            } else {
                $records = BaseElement::get();
            }

            $records = $records->filter('ParentID', $this->ParentID);

            $this->Sort = $records->max('Sort') + 1;
        }
    }
public function onBeforeWrite ( ) { parent :: onBeforeWrite ( ) ; if ( ! $ this -> Sort ) { if ( $ this -> hasExtension ( Versioned :: class ) ) { $ records = Versioned :: get_by_stage ( BaseElement :: class , Versioned :: DRAFT ) ; } else { $ records = BaseElement :: get ( ) ; } $ records = $ records -> filter ( 'ParentID' , $ this -> ParentID ) ; $ this -> Sort = $ records -> max ( 'Sort' ) + 1 ; } }
Increment the sort order if one hasn't been already defined. This
ensures that new elements are created at the end of the list by default.

{@inheritDoc}
Increment the sort order if one hasn t been already defined . This ensures that new elements are created at the end of the list by default .