Permalink
Browse files

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

ENHANCEMENT: Add language dropdown for link form
  • Loading branch information...
2 parents 1bb392c + 846398d commit 5270364efc8ae92fe1d146e4e4aff46e4db116c3 @chillu chillu committed Nov 15, 2012
View
@@ -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 @@ function init() {
if(
$requestLocale && $page && $page->hasExtension('Translatable')
&& $page->Locale != $requestLocale
+ && $req->latestParam('Action') != 'EditorToolbar'
) {
$transPage = $page->getTranslation($requestLocale);
if($transPage) {
@@ -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 @@ function __construct($name, $title, $excludeLocales = array(),
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;
+ }
+
}
@@ -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));

0 comments on commit 5270364

Please sign in to comment.