From 616a123ddcb80b2c50f227deabf2f17f0d1f5c9d Mon Sep 17 00:00:00 2001 From: Niklas Forsdahl Date: Mon, 12 Nov 2012 15:42:48 +0200 Subject: [PATCH 1/2] ENHANCEMENT: Add language dropdown for link form Adds a language dropdown to the link form in the html editor, so that linking to pages in different locales is possible. --- _config.php | 3 +- .../TranslatableCMSMainExtension.php | 1 + .../TranslatableEditorToolbarExtension.php | 12 ++++++ code/forms/LanguageDropdownField.php | 29 ++++++++++++++ javascript/HtmlEditorField.Translatable.js | 39 +++++++++++++++++++ 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 code/controller/TranslatableEditorToolbarExtension.php create mode 100644 javascript/HtmlEditorField.Translatable.js 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..ffc9daf --- /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 From 846398d128835adfe56d369f71ca5fca27d2cc58 Mon Sep 17 00:00:00 2001 From: Niklas Forsdahl Date: Mon, 12 Nov 2012 15:58:17 +0200 Subject: [PATCH 2/2] Correct translation for language dropdown field in link form --- code/controller/TranslatableEditorToolbarExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/controller/TranslatableEditorToolbarExtension.php b/code/controller/TranslatableEditorToolbarExtension.php index ffc9daf..d76e48e 100644 --- a/code/controller/TranslatableEditorToolbarExtension.php +++ b/code/controller/TranslatableEditorToolbarExtension.php @@ -3,7 +3,7 @@ class TranslatableEditorToolbarExtension extends DataExtension { function updateLinkForm(&$form) { - $field = new LanguageDropdownField('Language', _t('PageURLSelectionField.LANGUAGE', 'Language')); + $field = new LanguageDropdownField('Language', _t('CMSMain.LANGUAGEDROPDOWNLABEL', 'Language')); $field->setForm($form); $form->Fields()->insertBefore($field, 'internal'); Requirements::javascript('translatable/javascript/HtmlEditorField.Translatable.js');