diff --git a/_config.php b/_config.php index 22136e1..9ffd92d 100644 --- a/_config.php +++ b/_config.php @@ -1,4 +1,5 @@ hasExtension('Translatable') && $page->Locale != $requestLocale + && $req->latestParam('Action') != 'EditorToolbar' ) { $transPage = $page->getTranslation($requestLocale); if($transPage) { diff --git a/code/controller/TranslatableEditorToolbarExtension.php b/code/controller/TranslatableEditorToolbarExtension.php new file mode 100644 index 0000000..d76e48e --- /dev/null +++ b/code/controller/TranslatableEditorToolbarExtension.php @@ -0,0 +1,12 @@ +setForm($form); + $form->Fields()->insertBefore($field, 'internal'); + Requirements::javascript('translatable/javascript/HtmlEditorField.Translatable.js'); + } + +} \ No newline at end of file diff --git a/code/forms/LanguageDropdownField.php b/code/forms/LanguageDropdownField.php index 65b1d81..807dfea 100755 --- a/code/forms/LanguageDropdownField.php +++ b/code/forms/LanguageDropdownField.php @@ -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; + } + } \ No newline at end of file diff --git a/javascript/HtmlEditorField.Translatable.js b/javascript/HtmlEditorField.Translatable.js new file mode 100644 index 0000000..0aec82d --- /dev/null +++ b/javascript/HtmlEditorField.Translatable.js @@ -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)); \ No newline at end of file