src/Extension/MultisitesSiteTreeExtension.php
public function onBeforeWrite()
    {
        if ($this->owner instanceof Site) {
            if (!$this->owner->ID) {
                // Initialise a new Site to the top level
                $this->owner->SiteID   = 0;
                $this->owner->ParentID = 0;
            }
            return;
        }

        // Set the SiteID (and ParentID if required) for all new pages.
        if (!$this->owner->ID) {
            $parent = $this->owner->Parent();
            if ($parent && $parent->exists()) {
                // Page is beneath a Site
                if ($parent instanceof Site) {
                    $this->owner->SiteID = $parent->ID;
                } else {
                    $this->owner->SiteID = $parent->SiteID;
                }
            } else {
                // Create the page beneath the current Site
                $this->owner->SiteID   = Multisites::inst()->getDefaultSiteId();
                $this->owner->ParentID = $this->owner->SiteID;
            }
        }

        // Make sure SiteID is changed when site tree is reorganised.
        if ($this->owner->ID && $this->owner->isChanged('ParentID')) {
            // Get the new parent
            $parent = DataObject::get_by_id(SiteTree::class, $this->owner->ParentID);

            // Make sure the parent exists
            if ($parent) {
                // Recursively change SiteID for this and all child pages
                $siteId = ($parent instanceof Site) ? $parent->ID : $parent->SiteID;
                $this->owner->updateSiteID($siteId);
            }
        }
    }
public function onBeforeWrite()
    {
        if ($this->owner instanceof Site) {
            if (!$this->owner->ID) {
                // Initialise a new Site to the top level
                $this->owner->SiteID   = 0;
                $this->owner->ParentID = 0;
            }
            return;
        }

        // Set the SiteID (and ParentID if required) for all new pages.
        if (!$this->owner->ID) {
            $parent = $this->owner->Parent();
            if ($parent && $parent->exists()) {
                // Page is beneath a Site
                if ($parent instanceof Site) {
                    $this->owner->SiteID = $parent->ID;
                } else {
                    $this->owner->SiteID = $parent->SiteID;
                }
            } else {
                // Create the page beneath the current Site
                $this->owner->SiteID   = Multisites::inst()->getDefaultSiteId();
                $this->owner->ParentID = $this->owner->SiteID;
            }
        }

        // Make sure SiteID is changed when site tree is reorganised.
        if ($this->owner->ID && $this->owner->isChanged('ParentID')) {
            // Get the new parent
            $parent = DataObject::get_by_id(SiteTree::class, $this->owner->ParentID);

            // Make sure the parent exists
            if ($parent) {
                // Recursively change SiteID for this and all child pages
                $siteId = ($parent instanceof Site) ? $parent->ID : $parent->SiteID;
                $this->owner->updateSiteID($siteId);
            }
        }
    }
public function onBeforeWrite ( ) { if ( $ this -> owner instanceof Site ) { if ( ! $ this -> owner -> ID ) { // Initialise a new Site to the top level $ this -> owner -> SiteID = 0 ; $ this -> owner -> ParentID = 0 ; } return ; } // Set the SiteID (and ParentID if required) for all new pages. if ( ! $ this -> owner -> ID ) { $ parent = $ this -> owner -> Parent ( ) ; if ( $ parent && $ parent -> exists ( ) ) { // Page is beneath a Site if ( $ parent instanceof Site ) { $ this -> owner -> SiteID = $ parent -> ID ; } else { $ this -> owner -> SiteID = $ parent -> SiteID ; } } else { // Create the page beneath the current Site $ this -> owner -> SiteID = Multisites :: inst ( ) -> getDefaultSiteId ( ) ; $ this -> owner -> ParentID = $ this -> owner -> SiteID ; } } // Make sure SiteID is changed when site tree is reorganised. if ( $ this -> owner -> ID && $ this -> owner -> isChanged ( 'ParentID' ) ) { // Get the new parent $ parent = DataObject :: get_by_id ( SiteTree :: class , $ this -> owner -> ParentID ) ; // Make sure the parent exists if ( $ parent ) { // Recursively change SiteID for this and all child pages $ siteId = ( $ parent instanceof Site ) ? $ parent -> ID : $ parent -> SiteID ; $ this -> owner -> updateSiteID ( $ siteId ) ; } } }
Keep the SiteID field consistent.
Keep the SiteID field consistent .