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

        $defaults = $this->owner->config()->defaults;
        $URLSegment = $this->owner->URLSegment;
        // If there is no URLSegment set, generate one from Title
        if((!$URLSegment || $URLSegment == $defaults['URLSegment']) && $this->owner->Title != $defaults['Title']) {
            $URLSegment = $this->generateURLSegment($this->owner->Title);
        } else if($this->owner->isChanged('URLSegment')) {
            // Make sure the URLSegment is valid for use in a URL
            $segment = preg_replace('/[^A-Za-z0-9]+/','-',$this->owner->URLSegment);
            $segment = preg_replace('/-+/','-',$segment);
            // If after sanitising there is no URLSegment, give it a reasonable default
            if(!$segment) {
                $segment = $this->fallbackUrl();
            }
            $URLSegment = $segment;
        }
        // Ensure that this object has a non-conflicting URLSegment value.
        $count = 2;
        $ID = $this->owner->ID;
        while($this->lookForExistingURLSegment($URLSegment, $ID)) {
            $URLSegment = preg_replace('/-[0-9]+$/', null, $URLSegment) . '-' . $count;
            $count++;
        }

        $this->owner->URLSegment = $URLSegment;
    }