function field_valid_language

Ensures that a given language code is valid.

Checks whether the given language code is one of the enabled language codes. Otherwise, it returns the current, global language code; or the site's default language code, if the additional parameter $default is TRUE.

Parameters

$langcode: The language code to validate.

$default: Whether to return the default language code or the current language code in case $langcode is invalid.

Return value

A valid language code.

Related topics

3 calls to field_valid_language()
field_attach_form in drupal/core/modules/field/field.attach.inc
Adds form elements for all fields for an entity to a form structure.
field_language in drupal/core/modules/field/field.multilingual.inc
Returns the display language code for the fields attached to the given entity.
TranslationTest::testFieldFormTranslationRevisions in drupal/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php
Tests field translations when creating a new revision.

File

drupal/core/modules/field/field.multilingual.inc, line 262
Functions implementing Field API multilingual support.

Code

function field_valid_language($langcode, $default = TRUE) {
  $languages = field_content_languages();
  if (in_array($langcode, $languages)) {
    return $langcode;
  }
  return $default ? language_default()->langcode : language(LANGUAGE_TYPE_CONTENT)->langcode;
}