public function duplicateToSubsitePrep($subsiteID, $includeChildren)
    {
        if (is_object($subsiteID)) {
            $subsiteID = $subsiteID->ID;
        }

        return SubsiteState::singleton()
            ->withState(function (SubsiteState $newState) use ($subsiteID, $includeChildren) {
                $newState->setSubsiteId($subsiteID);

                /** @var SiteTree $page */
                $page = $this->owner;

                try {
                    // We have no idea what the ParentID should be, but it shouldn't be the same as it was since
                    // we're now in a different subsite. As a workaround use the url-segment and subsite ID.
                    if ($page->Parent()) {
                        $parentSeg = $page->Parent()->URLSegment;
                        $newParentPage = Page::get()->filter('URLSegment', $parentSeg)->first();
                        $originalParentID = $page->ParentID;
                        if ($newParentPage) {
                            $page->ParentID = (int) $newParentPage->ID;
                        } else {
                            // reset it to the top level, so the user can decide where to put it
                            $page->ParentID = 0;
                        }
                    }

                    // Disable query filtering by subsite during actual duplication
                    $originalFilter = Subsite::$disable_subsite_filter;
                    Subsite::disable_subsite_filter(true);

                    return $includeChildren ? $page->duplicateWithChildren() : $page->duplicate(false);
                } finally {
                    Subsite::disable_subsite_filter($originalFilter);

                    // Re-set the original parent ID for the current page
                    $page->ParentID = $originalParentID;
                }
            });
    }
public function duplicateToSubsitePrep($subsiteID, $includeChildren)
    {
        if (is_object($subsiteID)) {
            $subsiteID = $subsiteID->ID;
        }

        return SubsiteState::singleton()
            ->withState(function (SubsiteState $newState) use ($subsiteID, $includeChildren) {
                $newState->setSubsiteId($subsiteID);

                /** @var SiteTree $page */
                $page = $this->owner;

                try {
                    // We have no idea what the ParentID should be, but it shouldn't be the same as it was since
                    // we're now in a different subsite. As a workaround use the url-segment and subsite ID.
                    if ($page->Parent()) {
                        $parentSeg = $page->Parent()->URLSegment;
                        $newParentPage = Page::get()->filter('URLSegment', $parentSeg)->first();
                        $originalParentID = $page->ParentID;
                        if ($newParentPage) {
                            $page->ParentID = (int) $newParentPage->ID;
                        } else {
                            // reset it to the top level, so the user can decide where to put it
                            $page->ParentID = 0;
                        }
                    }

                    // Disable query filtering by subsite during actual duplication
                    $originalFilter = Subsite::$disable_subsite_filter;
                    Subsite::disable_subsite_filter(true);

                    return $includeChildren ? $page->duplicateWithChildren() : $page->duplicate(false);
                } finally {
                    Subsite::disable_subsite_filter($originalFilter);

                    // Re-set the original parent ID for the current page
                    $page->ParentID = $originalParentID;
                }
            });
    }
public function duplicateToSubsitePrep ( $ subsiteID , $ includeChildren ) { if ( is_object ( $ subsiteID ) ) { $ subsiteID = $ subsiteID -> ID ; } return SubsiteState :: singleton ( ) -> withState ( function ( SubsiteState $ newState ) use ( $ subsiteID , $ includeChildren ) { $ newState -> setSubsiteId ( $ subsiteID ) ; /** @var SiteTree $page */ $ page = $ this -> owner ; try { // We have no idea what the ParentID should be, but it shouldn't be the same as it was since // we're now in a different subsite. As a workaround use the url-segment and subsite ID. if ( $ page -> Parent ( ) ) { $ parentSeg = $ page -> Parent ( ) -> URLSegment ; $ newParentPage = Page :: get ( ) -> filter ( 'URLSegment' , $ parentSeg ) -> first ( ) ; $ originalParentID = $ page -> ParentID ; if ( $ newParentPage ) { $ page -> ParentID = ( int ) $ newParentPage -> ID ; } else { // reset it to the top level, so the user can decide where to put it $ page -> ParentID = 0 ; } } // Disable query filtering by subsite during actual duplication $ originalFilter = Subsite :: $ disable_subsite_filter ; Subsite :: disable_subsite_filter ( true ) ; return $ includeChildren ? $ page -> duplicateWithChildren ( ) : $ page -> duplicate ( false ) ; } finally { Subsite :: disable_subsite_filter ( $ originalFilter ) ; // Re-set the original parent ID for the current page $ page -> ParentID = $ originalParentID ; } } ) ; }
Does the basic duplication, but doesn't write anything this means we can subclass this easier and do more
complex relation duplication.

Note that when duplicating including children, everything is written.

@param Subsite|int $subsiteID
@param bool $includeChildren
@return SiteTree
Does the basic duplication but doesn t write anything this means we can subclass this easier and do more complex relation duplication .