MINOR: Merged translatable URL handling fixes from trunk

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@76035 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2009-05-04 23:47:47 +00:00
parent ea085677a6
commit fafca08c44
3 changed files with 24 additions and 18 deletions

View File

@ -1008,7 +1008,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
$count++;
$this->URLSegment = ereg_replace('-[0-9]+$','', $this->URLSegment) . "-$count";
}
DataObject::set_context_obj(null);
// If the URLSegment has been changed, rewrite links
@ -1079,7 +1079,16 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
public static function get_by_url($urlSegment, $extraFilter = "", $cache = true, $orderby = "") {
$filter = sprintf("`SiteTree`.URLSegment = '%s'", Convert::raw2sql($urlSegment));
if($extraFilter) $filter .= " AND $extraFilter";
return DataObject::get_one("SiteTree", $filter, $cache, $orderby);
$matchedPage = DataObject::get_one("SiteTree", $filter, $cache, $orderby);
if($matchedPage) {
return $matchedPage;
} else {
$alternativeMatches = singleton('SiteTree')->extend('alternateGetByUrl', $urlSegment, $extraFilter, $cache, $orderby);
if($alternativeMatches) foreach($alternativeMatches as $alternativeMatch) {
if($alternativeMatch) return $alternativeMatch;
}
return false;
}
}
/**

View File

@ -634,6 +634,10 @@ class Translatable extends DataObjectDecorator {
* nested pages accessible in a translated CMS page tree.
* It would be more userfriendly to grey out untranslated pages,
* but this involves complicated special cases in AllChildrenIncludingDeleted().
*
* {@link SiteTree->onBeforeWrite()} will ensure that each translation will get
* a unique URL across languages, by means of {@link SiteTree::get_by_url()}
* and {@link Translatable->alternateGetByURL()}.
*/
function onBeforeWrite() {
// If language is not set explicitly, set it to current_locale.
@ -659,18 +663,6 @@ class Translatable extends DataObjectDecorator {
}
}
// Specific logic for SiteTree subclasses.
// Append language to URLSegment to disambiguate URLs, meaning "myfrenchpage"
// will save as "myfrenchpage-fr" (only if we're not in the "default language").
// Its bad SEO to have multiple resources with different content (=language) under the same URL.
if($this->owner->hasField('URLSegment')) {
if(!$this->owner->ID && $this->owner->Locale != Translatable::default_locale()) {
$SQL_URLSegment = Convert::raw2sql($this->owner->URLSegment);
$existingOriginalPage = Translatable::get_one_by_lang('SiteTree', Translatable::default_locale(), "`URLSegment` = '{$SQL_URLSegment}'");
if($existingOriginalPage) $this->owner->URLSegment .= "-{$this->owner->Locale}";
}
}
// Has to be limited to the default locale, the assumption is that the "page type"
// dropdown is readonly on all translations.
if($this->owner->ID && $this->owner->Locale == Translatable::default_locale()) {
@ -784,6 +776,10 @@ class Translatable extends DataObjectDecorator {
if($originalRecord && $isTranslationMode) {
$originalLangID = Session::get($this->owner->ID . '_originalLangID');
// Remove parent page dropdown
$fields->removeByName("ParentType");
$fields->removeByName("ParentID");
$translatableFieldNames = $this->getTranslatableFields();
$allDataFields = $fields->dataFields();
@ -1230,4 +1226,4 @@ class Translatable_Transformation extends FormTransformation {
}
?>
?>

View File

@ -174,12 +174,13 @@ class TranslatableTest extends FunctionalTest {
);
}
function testTranslationCanHaveSameURLSegment() {
function testTranslationCantHaveSameURLSegmentAcrossLanguages() {
$origPage = $this->objFromFixture('Page', 'testpage_en');
$translatedPage = $origPage->createTranslation('de_DE');
$translatedPage->URLSegment = 'testpage';
$this->assertEquals($origPage->URLSegment, $translatedPage->URLSegment);
$translatedPage->write();
$this->assertNotEquals($origPage->URLSegment, $translatedPage->URLSegment);
}
function testUpdateCMSFieldsOnSiteTree() {