public function duplicateWithChildren()
    {
        /** @var SiteTree $clone */
        $clone = $this->duplicate();
        $children = $this->AllChildren();

        if ($children) {
            /** @var SiteTree $child */
            $sort = 0;
            foreach ($children as $child) {
                $childClone = method_exists($child, 'duplicateWithChildren')
                    ? $child->duplicateWithChildren()
                    : $child->duplicate();
                $childClone->ParentID = $clone->ID;
                //retain sort order by manually setting sort values
                $childClone->Sort = ++$sort;
                $childClone->write();
            }
        }

        return $clone;
    }
public function duplicateWithChildren()
    {
        /** @var SiteTree $clone */
        $clone = $this->duplicate();
        $children = $this->AllChildren();

        if ($children) {
            /** @var SiteTree $child */
            $sort = 0;
            foreach ($children as $child) {
                $childClone = method_exists($child, 'duplicateWithChildren')
                    ? $child->duplicateWithChildren()
                    : $child->duplicate();
                $childClone->ParentID = $clone->ID;
                //retain sort order by manually setting sort values
                $childClone->Sort = ++$sort;
                $childClone->write();
            }
        }

        return $clone;
    }
public function duplicateWithChildren ( ) { /** @var SiteTree $clone */ $ clone = $ this -> duplicate ( ) ; $ children = $ this -> AllChildren ( ) ; if ( $ children ) { /** @var SiteTree $child */ $ sort = 0 ; foreach ( $ children as $ child ) { $ childClone = method_exists ( $ child , 'duplicateWithChildren' ) ? $ child -> duplicateWithChildren ( ) : $ child -> duplicate ( ) ; $ childClone -> ParentID = $ clone -> ID ; //retain sort order by manually setting sort values $ childClone -> Sort = ++ $ sort ; $ childClone -> write ( ) ; } } return $ clone ; }
Duplicates each child of this node recursively and returns the top-level duplicate node.

@return static The duplicated object
Duplicates each child of this node recursively and returns the top - level duplicate node .