/** * Returns a LanguageDropdownField instance used as a default for form * scaffolding. * * @param string $title Optional. Localized title of the generated instance * @param array $params Optional. * * @return FormField * * @author Sebastian Diel <*****@*****.**> * @since 20.03.2013 */ public function scaffoldFormField($title = null, $params = null) { if (is_null($title)) { $title = _t('SilvercartConfig.TRANSLATION'); } $instance = null; $alreadyTranslatedLocales = array(); $translatingClass = 'SiteTree'; if (array_key_exists('object', $params)) { $translatingClass = $params['object']->ClassName; $instance = $params['object']; } if ($instance) { $alreadyTranslatedLocales = $instance->getTranslatedLocales(); unset($alreadyTranslatedLocales[$instance->Locale]); } $localeDropdown = new LanguageDropdownField($this->name, $title, $alreadyTranslatedLocales, $translatingClass, 'Locale-Native', $instance); $currentLocale = Translatable::get_current_locale(); $localesWithTitle = $localeDropdown->getSource(); $usedLocalesWithTitle = Translatable::get_existing_content_languages('SiteTree'); $languageList = array(); $usedLanguageList = array(); foreach ($localesWithTitle as $locale => $localeTitle) { if (is_array($localeTitle)) { foreach ($localeTitle as $locale2 => $title2) { $title2 = SilvercartLanguageHelper::getLanguageDisplayTitle($locale2, $currentLocale); if (array_key_exists($locale2, $usedLocalesWithTitle)) { $usedLanguageList[$locale2] = $title2; unset($languageList[$locale2]); } else { $languageList[$locale2] = $title2; } } } else { $localeTitle = SilvercartLanguageHelper::getLanguageDisplayTitle($locale, $currentLocale); if (array_key_exists($locale, $usedLocalesWithTitle)) { $usedLanguageList[$locale] = $localeTitle; unset($languageList[$locale]); } else { $languageList[$locale] = $localeTitle; } } } asort($languageList); if (count($usedLanguageList)) { asort($usedLanguageList); $languageList = array(_t('Form.LANGAVAIL', "Available languages") => $usedLanguageList, _t('Form.LANGAOTHER', "Other languages") => $languageList); } $localeDropdown->setSource($languageList); return $localeDropdown; }
/** * Creates and returns the language dropdown field * * @param DataObject $dataObj DataObject to get dropdown for * @param string $translatingClass Context class of the LanguageDropdownField * @param string $fieldName Name of the LanguageDropdownField * * @return LanguageDropdownField * * @author Sebastian Diel <*****@*****.**> * @since 21.12.2015 */ public static function prepareLanguageDropdownField($dataObj, $translatingClass = null, $fieldName = 'Locale') { $instance = null; $alreadyTranslatedLocales = array(); if (is_null($translatingClass)) { $translatingClass = $dataObj->ClassName; $instance = $dataObj; } if ($instance) { $alreadyTranslatedLocales = $instance->getTranslatedLocales(); unset($alreadyTranslatedLocales[$instance->Locale]); } $localeDropdown = new LanguageDropdownField($fieldName, _t('SilvercartConfig.TRANSLATION'), $alreadyTranslatedLocales, $translatingClass, 'Locale-Native', $instance); $currentLocale = Translatable::get_current_locale(); $localesWithTitle = $localeDropdown->getSource(); $usedLocalesWithTitle = Translatable::get_existing_content_languages('SiteTree'); $languageList = array(); $usedLanguageList = array(); foreach ($localesWithTitle as $locale => $title) { if (is_array($title)) { foreach ($title as $locale2 => $title2) { $title2 = self::getLanguageDisplayTitle($locale2, $currentLocale); if (array_key_exists($locale2, $usedLocalesWithTitle)) { $usedLanguageList[$locale2] = $title2; unset($languageList[$locale2]); } else { $languageList[$locale2] = $title2; } } } else { $title = self::getLanguageDisplayTitle($locale, $currentLocale); if (array_key_exists($locale, $usedLocalesWithTitle)) { $usedLanguageList[$locale] = $title; unset($languageList[$locale]); } else { $languageList[$locale] = $title; } } } asort($languageList); if (count($usedLanguageList)) { asort($usedLanguageList); $languageList = array(_t('Form.LANGAVAIL', "Available languages") => $usedLanguageList, _t('Form.LANGAOTHER', "Other languages") => $languageList); } $localeDropdown->setSource($languageList); $localeDropdown->setValue($dataObj->Locale); return $localeDropdown; }