all_locales; if(isset($allLocales[Translatable::default_locale()])) { unset($allLocales[Translatable::default_locale()]); } // Limit to allowed locales if defined // Check for canTranslate() if an $instance is given $allowedLocales = Translatable::get_allowed_locales(); foreach($allLocalesWithTitle as $locale => $localeTitle) { if( ($allowedLocales && !in_array($locale, $allowedLocales)) || ($excludeLocales && in_array($locale, $excludeLocales)) || ($usedLocalesWithTitle && array_key_exists($locale, $usedLocalesWithTitle)) ) { unset($allLocalesWithTitle[$locale]); } } // instance specific permissions foreach($allLocalesWithTitle as $locale => $localeTitle) { if($instance && !$instance->canTranslate(null, $locale)) { unset($allLocalesWithTitle[$locale]); } } foreach($usedLocalesWithTitle as $locale => $localeTitle) { if($instance && !$instance->canTranslate(null, $locale)) { unset($usedLocalesWithTitle[$locale]); } } // Sort by title (array value) asort($allLocalesWithTitle); if(count($usedLocalesWithTitle)) { asort($usedLocalesWithTitle); $source = array( _t('Form.LANGAVAIL', "Available languages") => $usedLocalesWithTitle, _t('Form.LANGAOTHER', "Other languages") => $allLocalesWithTitle ); } else { $source = $allLocalesWithTitle; } parent::__construct($name, $title, $source); } function Type() { return 'languagedropdown dropdown'; } public function getAttributes() { return array_merge( parent::getAttributes(), array( 'data-locale-url' => $this->Link('getLocaleForObject'), 'data-translation-url' => $this->Link('getURLForTranslationAndLocale') ) ); } /** * 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) && $class::has_extension('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; } /** * @param $request * @return string */ function getURLForTranslationAndLocale($request) { $id = (int)$request->requestVar('id'); $class = Convert::raw2sql($request->requestVar('class')); $requestedLocale = Convert::raw2sql($request->requestVar('requestedLocale')); $url = ''; if ($id && $class && class_exists($class) && $class::has_extension('Translatable')) { if(($record = $class::get()->byId($id)) && ($translation = $record->getTranslations($requestedLocale)->First())) { $controller = $translation instanceOf SiteTree ? singleton('CMSPageEditController') : Controller::curr(); $url = Controller::join_links($controller->Link('show'), $translation->ID, '?locale=' . $translation->Locale); } } return $url; } }