Version: current

     1 <?php
     2 /**
     3  * An extension to dropdown field, pre-configured to list languages.
     4  * The languages already used in the site will be on top.
     5  * @package forms
     6  * @subpackage fields-relational
     7  */
     8 class LanguageDropdownField extends GroupedDropdownField {
     9
    10     /**
    11      * Create a new LanguageDropdownField
    12      * @param string $name
    13      * @param string $title
    14      * @param array $excludeLocales List of locales that won't be included
    15      * @param string $translatingClass Name of the class with translated instances where to look for used languages
    16      * @param string $list Indicates the source language list. Can be either Common-English, Common-Native, Locale-English, Locale-Native
    17      */
    18     function __construct($name, $title, $excludeLocales = array(), $translatingClass = 'SiteTree', $list = 'Common-English', $instance = null) {
    19         $usedLocalesWithTitle = Translatable::get_existing_content_languages($translatingClass);
    20         $usedLocalesWithTitle = array_diff_key($usedLocalesWithTitle, $excludeLocales);
    21
    22         if('Common-English' == $list) $allLocalesWithTitle = i18n::get_common_languages();
    23         else if('Common-Native' == $list) $allLocalesWithTitle = i18n::get_common_languages(true);
    24         else if('Locale-English' == $list) $allLocalesWithTitle = i18n::get_common_locales();
    25         else if('Locale-Native' == $list) $allLocalesWithTitle = i18n::get_common_locales(true);
    26         else $allLocalesWithTitle = i18n::get_locale_list();
    27
    28         if(isset($allLocales[Translatable::default_locale()])) unset($allLocales[Translatable::default_locale()]);
    29
    30         // Limit to allowed locales if defined
    31         // Check for canTranslate() if an $instance is given
    32         $allowedLocales = Translatable::get_allowed_locales();
    33         foreach($allLocalesWithTitle as $locale => $localeTitle) {
    34             if(
    35                 ($allowedLocales && !in_array($locale, $allowedLocales))
    36                 || ($excludeLocales && in_array($locale, $excludeLocales))
    37                 || ($usedLocalesWithTitle && array_key_exists($locale, $usedLocalesWithTitle))
    38             ) {
    39                 unset($allLocalesWithTitle[$locale]);
    40             }
    41
    42             if($excludeLocales && in_array($locale, $excludeLocales)) {
    43                 unset($usedLocalesWithTitle[$locale]);
    44             }
    45         }
    46         // instance specific permissions
    47         foreach($allLocalesWithTitle as $locale => $localeTitle) {
    48             if($instance && !$instance->canTranslate(null, $locale)) {
    49                 unset($allLocalesWithTitle[$locale]);
    50             }
    51         }
    52         foreach($usedLocalesWithTitle as $locale => $localeTitle) {
    53             if($instance && !$instance->canTranslate(null, $locale)) {
    54                 unset($usedLocalesWithTitle[$locale]);
    55             }
    56         }
    57
    58         // Sort by title (array value)
    59         asort($allLocalesWithTitle);
    60
    61         if(count($usedLocalesWithTitle)) {
    62             asort($usedLocalesWithTitle);
    63             $source = array(
    64                 _t('Form.LANGAVAIL', "Available languages") => $usedLocalesWithTitle,
    65                 _t('Form.LANGAOTHER', "Other languages") => $allLocalesWithTitle
    66             );
    67         } else {
    68             $source = $allLocalesWithTitle;
    69         }
    70
    71         parent::__construct($name, $title, $source);
    72     }
    73 }
    74
    75 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -