You are here

function _languagefield_options in Custom Language field 7

Helper function to generate the language options list.

static $predefined: array of language subsets.

4 calls to _languagefield_options()
languagefield_feeds_set_target in ./languagefield.feeds.inc
Implements feed_set_target().
languagefield_field_prepare_view in ./languagefield.module
Implements hook_field_prepare_view().
languagefield_field_settings_form in ./languagefield.module
Implements hook_field_settings_form().
languagefield_options_list in ./languagefield.module
Implements hook_options_list(). Returns 'all' or 'enabled' languages, according to field settings.

File

./languagefield.module, line 480
Provides a list of languages from ISO 639 standard. All variables of a $language-object can be found here: http://api.drupal.org/api/drupal/developer--globals.php/global/language

Code

function _languagefield_options($subsets = NULL, $langcode = LANGUAGE_NONE) {
  static $predefined = NULL;

  // @see _locale_prepare_predefined_list()
  require_once DRUPAL_ROOT . '/includes/iso.inc';
  $allsets = array(
    LANGUAGE_NONE => TRUE,
    LANGUAGEFIELD_LANGUAGES_ALL => TRUE,
    LANGUAGEFIELD_LANGUAGES_ENABLED => TRUE,
    LANGUAGEFIELD_LANGUAGES_INSTALLED => TRUE,
    // The following values are copied from D8 LanguageConfiguration::getDefaultOptions().
    LANGUAGEFIELD_SITE_DEFAULT => TRUE,
    LANGUAGEFIELD_CURRENT_INTERFACE => TRUE,
    LANGUAGEFIELD_AUTHORS_DEFAULT => TRUE,
  );

  // oct 2014: choice of languages from radio to checkbox.
  if (!$subsets) {
    $subsets = $allsets;
  }
  elseif (!is_array($subsets)) {
    $subsets = array(
      $subsets => $subsets,
    );
  }

  // Use a cache for each user-language.
  if (!isset($predefined[$langcode])) {
    foreach ($allsets as $subset => $active) {

      // Get the untranslated language list, using the (array-format) of _locale_get_predefined_list().
      // @TODO: One day we should use the (objects-)format of language_list().
      switch ($subset) {
        case LANGUAGE_NONE:
          $predefined[$langcode][$subset][LANGUAGE_NONE]['value'] = LANGUAGE_NONE;
          $predefined[$langcode][$subset][LANGUAGE_NONE]['langcode'] = LANGUAGE_NONE;
          $predefined[$langcode][$subset][LANGUAGE_NONE]['name'] = t('Language neutral');
          $predefined[$langcode][$subset][LANGUAGE_NONE]['native'] = '';
          break;
        case LANGUAGEFIELD_SITE_DEFAULT:
        case LANGUAGEFIELD_CURRENT_INTERFACE:
        case LANGUAGEFIELD_AUTHORS_DEFAULT:
          $language = _languagefield_getLanguageConfigurationOptions($subset);
          $predefined[$langcode][$subset][$subset]['value'] = $subset;
          $predefined[$langcode][$subset][$subset]['langcode'] = $subset;
          $predefined[$langcode][$subset][$subset]['name'] = t($language[$subset]);
          $predefined[$langcode][$subset][$subset]['native'] = '';
          break;

        // The following are from Languagefield module.
        case LANGUAGEFIELD_LANGUAGES_INSTALLED:
        case LANGUAGEFIELD_LANGUAGES_ENABLED:
          $predefined[$langcode][$subset] = array();

          // Get all possible languages with English and native name.
          $languages = language_list();
          foreach ($languages as $language) {
            if ($language->enabled || $subset == LANGUAGEFIELD_LANGUAGES_INSTALLED) {

              // Prepare for D8. The next line can be removed in D8. See http://drupal.org/node/1399806
              $language->langcode = isset($language->langcode) ? $language->langcode : $language->language;
              $predefined[$langcode][$subset][$language->langcode]['value'] = $language->langcode;
              $predefined[$langcode][$subset][$language->langcode]['langcode'] = $language->langcode;
              $predefined[$langcode][$subset][$language->langcode]['name'] = t($language->name);
              $predefined[$langcode][$subset][$language->langcode]['native'] = isset($language->native) ? $language->native : '';
            }
          }
          break;
        case LANGUAGEFIELD_LANGUAGES_ALL:
        default:
          $subset = LANGUAGEFIELD_LANGUAGES_ALL;

          // @see _locale_prepare_predefined_list()
          $languages = _locale_get_predefined_list();
          foreach ($languages as $id => $language) {
            $predefined[$langcode][$subset][$id]['value'] = $id;
            $predefined[$langcode][$subset][$id]['langcode'] = $id;
            $predefined[$langcode][$subset][$id]['name'] = t($language[0]);
            $predefined[$langcode][$subset][$id]['native'] = isset($language[1]) ? $language[1] : $language[0];
          }
          break;
      }

      // Translate the language list.
      $t_options = $langcode != LANGUAGE_NONE ? array(
        'langcode' => $langcode,
      ) : array();
      foreach ($predefined[$langcode][$subset] as $key => $value) {

        // Translate the language name, if possible.
        $tname = t($value['name'], array(), $t_options);

        // Append native name in output, if possible.
        if (!empty($value['native']) && $tname != $value['native']) {
          $predefined[$langcode][$subset][$key]['name_native'] = t('!language (!native)', array(
            '!language' => $tname,
            '!native' => $value['native'],
          ));
        }
        else {
          $predefined[$langcode][$subset][$key]['name_native'] = $tname;
        }
      }
    }
  }

  // Compile the list of languages.
  $languages = array();
  foreach ($subsets as $subset => $active) {
    if ($active) {
      $languages += $predefined[$langcode][$subset];
    }
  }
  asort($languages);
  return $languages;
}