You are here

function languagefield_options_list in Custom Language field 7

Implements hook_options_list(). Returns 'all' or 'enabled' languages, according to field settings.

3 calls to languagefield_options_list()
languagefield_field_settings_form in ./languagefield.module
Implements hook_field_settings_form().
languagefield_handler_filter_locale_language::get_value_options in handlers/languagefield_handler_filter_locale_language.inc
Child classes should be used to override this function and set the 'value options', unless 'options callback' is defined as a valid function or static public method to generate these values.
MigrateLanguagefieldFieldHandler::prepare in ./languagefield.migrate.inc

File

./languagefield.module, line 395
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_list($field, $instance, $entity_type, $entity) {
  $settings =& $field['settings'];

  // oct 2012: old options for display in widget: move old data and hide old options, to be used in languagefield_theme_option().
  if (!empty($settings['include_native'])) {
    $settings['format']['name_native'] = 'name_native';
  }
  $all_languages = _languagefield_options($settings['enabled']);
  $languages = array_filter($settings['languages']);
  if (!empty($languages)) {
    $all_languages = array_intersect_key($all_languages, $languages);
  }
  if (!empty($settings['excluded_languages'])) {
    $all_languages = array_diff_key($all_languages, $settings['excluded_languages']);
  }
  foreach ($all_languages as $delta => $item) {
    $markup = _languagefield_theme_option($item, $settings);
    $all_languages[$delta] = $markup;
  }
  asort($all_languages);
  if (!empty($settings['groups'])) {
    $grouped_languages = array();
    $found_languages = array();
    $all_languages += array(
      'other' => t('Other languages'),
    );
    foreach (explode("\n", $settings['groups']) as $line) {
      if (strpos($line, '|') !== FALSE) {
        list($group, $langs) = explode('|', $line, 2);
        $langs = array_filter(array_map('trim', explode(',', $langs)));
        $langs = array_intersect_key($all_languages, array_combine($langs, $langs));
        $found_languages += $langs;
        $grouped_languages[$group] = $langs;
      }
      else {
        $langs = array_filter(array_map('trim', explode(',', $line)));
        if (!empty($langs)) {
          $langs = array_intersect_key($all_languages, array_combine($langs, $langs));
          $found_languages += (array) $langs;
          $grouped_languages += (array) $langs;
        }
      }
    }
    $missing_languages = array_diff_key($all_languages, $found_languages);
    foreach ($grouped_languages as $index => $options) {
      if (is_array($options)) {
        if (isset($options['other'])) {
          unset($options['other']);
          if ($missing_languages) {
            $grouped_languages[$index] = array_merge($grouped_languages[$index], $missing_languages);
            $missing_languages = FALSE;
          }
        }
      }
    }
    if (isset($grouped_languages['other'])) {
      unset($grouped_languages['other']);
      if ($missing_languages) {
        $grouped_languages = array_merge($grouped_languages, $missing_languages);
      }
    }
    return $grouped_languages;
  }
  else {
    return $all_languages;
  }
}