mirror of
https://github.com/silverstripe/silverstripe-translatable
synced 2024-10-22 11:05:59 +02:00
Merge pull request #67 from creamarketing/languagedropdown-in-linkform
ENHANCEMENT: Add language dropdown for link form
This commit is contained in:
commit
5270364efc
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
Object::add_extension('SiteTree', 'Translatable');
|
||||
Object::add_extension('SiteConfig', 'Translatable');
|
||||
Object::add_extension('LeftAndMain', 'TranslatableCMSMainExtension');
|
||||
Object::add_extension('LeftAndMain', 'TranslatableCMSMainExtension');
|
||||
Object::add_extension('HtmlEditorField_Toolbar', 'TranslatableEditorToolbarExtension');
|
@ -49,6 +49,7 @@ class TranslatableCMSMainExtension extends Extension {
|
||||
if(
|
||||
$requestLocale && $page && $page->hasExtension('Translatable')
|
||||
&& $page->Locale != $requestLocale
|
||||
&& $req->latestParam('Action') != 'EditorToolbar'
|
||||
) {
|
||||
$transPage = $page->getTranslation($requestLocale);
|
||||
if($transPage) {
|
||||
|
12
code/controller/TranslatableEditorToolbarExtension.php
Normal file
12
code/controller/TranslatableEditorToolbarExtension.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
class TranslatableEditorToolbarExtension extends DataExtension {
|
||||
|
||||
function updateLinkForm(&$form) {
|
||||
$field = new LanguageDropdownField('Language', _t('CMSMain.LANGUAGEDROPDOWNLABEL', 'Language'));
|
||||
$field->setForm($form);
|
||||
$form->Fields()->insertBefore($field, 'internal');
|
||||
Requirements::javascript('translatable/javascript/HtmlEditorField.Translatable.js');
|
||||
}
|
||||
|
||||
}
|
@ -76,4 +76,33 @@ class LanguageDropdownField extends GroupedDropdownField {
|
||||
function Type() {
|
||||
return 'languagedropdown dropdown';
|
||||
}
|
||||
|
||||
public function getAttributes() {
|
||||
return array_merge(
|
||||
parent::getAttributes(),
|
||||
array('data-locale-url' => $this->Link('getLocaleForObject'))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the locale for an object that has the Translatable extension.
|
||||
*
|
||||
* @return locale
|
||||
*/
|
||||
function getLocaleForObject() {
|
||||
$id = (int)$this->getRequest()->requestVar('id');
|
||||
$class = Convert::raw2sql($this->getRequest()->requestVar('class'));
|
||||
$locale = Translatable::get_current_locale();
|
||||
if ($id && $class && class_exists($class) && Object::has_extension($class, 'Translatable')) {
|
||||
// temporarily disable locale filter so that we won't filter out the object
|
||||
Translatable::disable_locale_filter();
|
||||
$object = DataObject::get_by_id($class, $id);
|
||||
Translatable::enable_locale_filter();
|
||||
if ($object) {
|
||||
$locale = $object->Locale;
|
||||
}
|
||||
}
|
||||
return $locale;
|
||||
}
|
||||
|
||||
}
|
39
javascript/HtmlEditorField.Translatable.js
Normal file
39
javascript/HtmlEditorField.Translatable.js
Normal file
@ -0,0 +1,39 @@
|
||||
(function($) {
|
||||
$.entwine('ss', function($){
|
||||
$('form.htmleditorfield-linkform').entwine({
|
||||
redraw: function() {
|
||||
this._super();
|
||||
// show language field for internal links
|
||||
var linkType = this.find(':input[name=LinkType]:checked').val(), list = ['internal', 'external', 'file', 'email'];
|
||||
if(linkType == 'internal') {
|
||||
this.find('.field#Language').show();
|
||||
}
|
||||
},
|
||||
updateFromEditor: function() {
|
||||
var data = this.getCurrentLink();
|
||||
if(data && data.internal) {
|
||||
// fetch locale for given sitetree object
|
||||
var localeField = $(this).find('#Form_EditorToolbarLinkForm_Language');
|
||||
$.ajax({
|
||||
url: $.path.addSearchParams(localeField.data('localeUrl'), {'id': data.internal, 'class': 'SiteTree'}),
|
||||
success: function(data) {
|
||||
localeField.val(data);
|
||||
localeField.change();
|
||||
localeField.trigger('liszt:updated');
|
||||
}
|
||||
});
|
||||
}
|
||||
this._super();
|
||||
}
|
||||
});
|
||||
|
||||
$('form.htmleditorfield-linkform #Form_EditorToolbarLinkForm_Language').entwine({
|
||||
onchange: function(e) {
|
||||
// reload tree with selected locale
|
||||
var treeDropdown = $(this).parents('form').find('#internal .treedropdown');
|
||||
treeDropdown.data('urlTree', $.path.addSearchParams(treeDropdown.data('urlTree').replace(/locale=[^&]*/, ''), 'locale='+$(this).val()));
|
||||
treeDropdown.loadTree();
|
||||
}
|
||||
});
|
||||
});
|
||||
}(jQuery));
|
Loading…
Reference in New Issue
Block a user