Fixed LanguageDropdownField (above Tree) to redirect to the correct url after an other locale has been selected

This commit is contained in:
Christian Bünte 2014-02-18 14:19:07 +01:00
parent f92139a2b7
commit 7530becfb0
3 changed files with 60 additions and 15 deletions

1
code/controller/TranslatableCMSMainExtension.php Normal file → Executable file
View File

@ -6,6 +6,7 @@ class TranslatableCMSMainExtension extends Extension {
private static $allowed_actions = array(
'createtranslation',
'LangForm'
);
function init() {

View File

@ -8,7 +8,8 @@
class LanguageDropdownField extends GroupedDropdownField {
private static $allowed_actions = array(
'getLocaleForObject'
'getLocaleForObject',
'getURLForTranslationAndLocale'
);
/**
@ -84,7 +85,10 @@ class LanguageDropdownField extends GroupedDropdownField {
public function getAttributes() {
return array_merge(
parent::getAttributes(),
array('data-locale-url' => $this->Link('getLocaleForObject'))
array(
'data-locale-url' => $this->Link('getLocaleForObject'),
'data-translation-url' => $this->Link('getURLForTranslationAndLocale')
)
);
}
@ -108,5 +112,24 @@ class LanguageDropdownField extends GroupedDropdownField {
}
return $locale;
}
/**
* @param $request
* @return string
*/
function getURLForTranslationAndLocale($request) {
$id = (int)$request->requestVar('id');
$class = Convert::raw2sql($request->requestVar('class'));
$requestedLocale = Convert::raw2sql($request->requestVar('requestedLocale'));
$url = '';
if ($id && $class && class_exists($class) && $class::has_extension('Translatable')) {
if(($record = $class::get()->byId($id)) && ($translation = $record->getTranslations($requestedLocale)->First())) {
$controller = $translation instanceOf SiteTree ? singleton('CMSPageEditController') : Controller::curr();
$url = Controller::join_links($controller->Link('show'), $translation->ID, '?locale=' . $translation->Locale);
}
}
return $url;
}
}

View File

@ -37,18 +37,39 @@
},
onchange: function(e) {
// Get new locale code
locale = {locale: $(e.target).val()};
// Check existing url
search = /locale=[^&]*/;
url = document.location.href;
if(url.match(search)) {
// Replace locale code
url = url.replace(search, $.param(locale));
} else {
// Add locale code
url = $.path.addSearchParams(url, locale);
}
var locale = {locale: $(e.target).val()};
var url = document.location.href;
// Check existing url
search = /locale=[^&]*/;
if(url.match(search)) {
// Replace locale code
url = url.replace(search, $.param(locale));
} else {
// Add locale code
url = $.path.addSearchParams(url, locale);
}
// Get id for translation and fix redirect url
var selectedTreeItem = $('.cms-tree').jstree('get_selected');
if(selectedTreeItem.length > 0) {
selectedTreeItem = selectedTreeItem.eq(0);
$.get(
$.path.addSearchParams(this.data('translationUrl'), {
'requestedLocale': $(e.target).val(),
'id': selectedTreeItem.data('id'),
'class': selectedTreeItem.data('pagetype')
}),
function(data) {
if(data != '') {
url = data;
}
$('.cms-container').loadPanel(url);
}
);
return false;
}
$('.cms-container').loadPanel(url);
return false;
}