original_string: public function onBeforeDelete()
      {
          if (Versioned::get_reading_mode() == 'Stage.Stage') {
              $firstVirtual = false;
              $allVirtual = $this->getVirtualElements();
  
              if ($this->getPublishedVirtualElements()->Count() > 0) {
                  // choose the first one
                  $firstVirtual = $this->getPublishedVirtualElements()->First();
                  $wasPublished = true;
              } elseif ($allVirtual->Count() > 0) {
                  // choose the first one
                  $firstVirtual = $this->getVirtualElements()->First();
                  $wasPublished = false;
              }
              if ($firstVirtual) {
                  $clone = $this->owner->duplicate(false);
  
                  // set clones values to first virtual's values
                  $clone->ParentID = $firstVirtual->ParentID;
                  $clone->Sort = $firstVirtual->Sort;
  
                  $clone->write();
                  if ($wasPublished) {
                      $clone->doPublish();
                      $firstVirtual->doUnpublish();
                  }
  
                  // clone has a new ID, so need to repoint
                  // all the other virtual elements
                  foreach ($allVirtual as $virtual) {
                      if ($virtual->ID == $firstVirtual->ID) {
                          continue;
                      }
                      $pub = false;
                      if ($virtual->isPublished()) {
                          $pub = true;
                      }
                      $virtual->LinkedElementID = $clone->ID;
                      $virtual->write();
                      if ($pub) {
                          $virtual->doPublish();
                      }
                  }
  
                  $firstVirtual->delete();
              }
          }
      }
code: public function onBeforeDelete()
      {
          if (Versioned::get_reading_mode() == 'Stage.Stage') {
              $firstVirtual = false;
              $allVirtual = $this->getVirtualElements();
  
              if ($this->getPublishedVirtualElements()->Count() > 0) {
                  // choose the first one
                  $firstVirtual = $this->getPublishedVirtualElements()->First();
                  $wasPublished = true;
              } elseif ($allVirtual->Count() > 0) {
                  // choose the first one
                  $firstVirtual = $this->getVirtualElements()->First();
                  $wasPublished = false;
              }
              if ($firstVirtual) {
                  $clone = $this->owner->duplicate(false);
  
                  // set clones values to first virtual's values
                  $clone->ParentID = $firstVirtual->ParentID;
                  $clone->Sort = $firstVirtual->Sort;
  
                  $clone->write();
                  if ($wasPublished) {
                      $clone->doPublish();
                      $firstVirtual->doUnpublish();
                  }
  
                  // clone has a new ID, so need to repoint
                  // all the other virtual elements
                  foreach ($allVirtual as $virtual) {
                      if ($virtual->ID == $firstVirtual->ID) {
                          continue;
                      }
                      $pub = false;
                      if ($virtual->isPublished()) {
                          $pub = true;
                      }
                      $virtual->LinkedElementID = $clone->ID;
                      $virtual->write();
                      if ($pub) {
                          $virtual->doPublish();
                      }
                  }
  
                  $firstVirtual->delete();
              }
          }
      }
code_tokens: public function onBeforeDelete ( ) { if ( Versioned :: get_reading_mode ( ) == 'Stage.Stage' ) { $ firstVirtual = false ; $ allVirtual = $ this -> getVirtualElements ( ) ; if ( $ this -> getPublishedVirtualElements ( ) -> Count ( ) > 0 ) { // choose the first one $ firstVirtual = $ this -> getPublishedVirtualElements ( ) -> First ( ) ; $ wasPublished = true ; } elseif ( $ allVirtual -> Count ( ) > 0 ) { // choose the first one $ firstVirtual = $ this -> getVirtualElements ( ) -> First ( ) ; $ wasPublished = false ; } if ( $ firstVirtual ) { $ clone = $ this -> owner -> duplicate ( false ) ; // set clones values to first virtual's values $ clone -> ParentID = $ firstVirtual -> ParentID ; $ clone -> Sort = $ firstVirtual -> Sort ; $ clone -> write ( ) ; if ( $ wasPublished ) { $ clone -> doPublish ( ) ; $ firstVirtual -> doUnpublish ( ) ; } // clone has a new ID, so need to repoint // all the other virtual elements foreach ( $ allVirtual as $ virtual ) { if ( $ virtual -> ID == $ firstVirtual -> ID ) { continue ; } $ pub = false ; if ( $ virtual -> isPublished ( ) ) { $ pub = true ; } $ virtual -> LinkedElementID = $ clone -> ID ; $ virtual -> write ( ) ; if ( $ pub ) { $ virtual -> doPublish ( ) ; } } $ firstVirtual -> delete ( ) ; } } }
docstring: Ensure that if there are elements that are virtualised from this element
  that we move the original element to replace one of the virtual elements
  
  But only if it's a delete not an unpublish
docstring_tokens: Ensure that if there are elements that are virtualised from this element that we move the original element to replace one of the virtual elements