Merge pull request #67 from creamarketing/languagedropdown-in-linkform

ENHANCEMENT: Add language dropdown for link form
This commit is contained in:
Ingo Schommer 2012-11-15 03:34:21 -08:00
commit 5270364efc
5 changed files with 83 additions and 1 deletions

View File

@ -2,3 +2,4 @@
Object::add_extension('SiteTree', 'Translatable');
Object::add_extension('SiteConfig', 'Translatable');
Object::add_extension('LeftAndMain', 'TranslatableCMSMainExtension');
Object::add_extension('HtmlEditorField_Toolbar', 'TranslatableEditorToolbarExtension');

View File

@ -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) {

View 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');
}
}

View File

@ -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;
}
}

View 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));