mirror of
https://github.com/silverstripe/silverstripe-translatable
synced 2024-10-22 09:05:59 +00:00
Fixed LanguageDropdownField (above Tree) to redirect to the correct url after an other locale has been selected
This commit is contained in:
parent
f92139a2b7
commit
7530becfb0
1
code/controller/TranslatableCMSMainExtension.php
Normal file → Executable file
1
code/controller/TranslatableCMSMainExtension.php
Normal file → Executable file
@ -6,6 +6,7 @@ class TranslatableCMSMainExtension extends Extension {
|
|||||||
|
|
||||||
private static $allowed_actions = array(
|
private static $allowed_actions = array(
|
||||||
'createtranslation',
|
'createtranslation',
|
||||||
|
'LangForm'
|
||||||
);
|
);
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
|
@ -8,7 +8,8 @@
|
|||||||
class LanguageDropdownField extends GroupedDropdownField {
|
class LanguageDropdownField extends GroupedDropdownField {
|
||||||
|
|
||||||
private static $allowed_actions = array(
|
private static $allowed_actions = array(
|
||||||
'getLocaleForObject'
|
'getLocaleForObject',
|
||||||
|
'getURLForTranslationAndLocale'
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -84,7 +85,10 @@ class LanguageDropdownField extends GroupedDropdownField {
|
|||||||
public function getAttributes() {
|
public function getAttributes() {
|
||||||
return array_merge(
|
return array_merge(
|
||||||
parent::getAttributes(),
|
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;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -37,18 +37,39 @@
|
|||||||
},
|
},
|
||||||
onchange: function(e) {
|
onchange: function(e) {
|
||||||
// Get new locale code
|
// Get new locale code
|
||||||
locale = {locale: $(e.target).val()};
|
var locale = {locale: $(e.target).val()};
|
||||||
|
var url = document.location.href;
|
||||||
// Check existing url
|
|
||||||
search = /locale=[^&]*/;
|
// Check existing url
|
||||||
url = document.location.href;
|
search = /locale=[^&]*/;
|
||||||
if(url.match(search)) {
|
if(url.match(search)) {
|
||||||
// Replace locale code
|
// Replace locale code
|
||||||
url = url.replace(search, $.param(locale));
|
url = url.replace(search, $.param(locale));
|
||||||
} else {
|
} else {
|
||||||
// Add locale code
|
// Add locale code
|
||||||
url = $.path.addSearchParams(url, locale);
|
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);
|
$('.cms-container').loadPanel(url);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user