3

We are creating nodes programmatically on submitting a form. The language of a node is an input value in the form. How can I get the langcode, when I have the Language name?

For example, user may type English. So i should convert it to "en"

If there an in-built function for that?

  • 1
    This is not clear to me: you let your user type any kind of language? Aren't you supposed to list all the languages which are available in your site in the first place? – ssibal Dec 18 '18 at 10:22
4

You can do it with this code.

// Language to search.
$find = 'French';

// Getting all the available languages.
$languages = \Drupal::service('language_manager')->getStandardLanguageList();

$key = key(array_filter($languages, function ($array) use ($find) {
  if (array_search($find, $array) === FALSE) {
    return FALSE;
  }
  return TRUE;
}));

// If you have the devel module installed you can use the dpm function.
dpm($key);
  • TBH, this adds nothing new to the already given answer... – leymannx Dec 14 '18 at 19:44
  • @leymannx the code in the other answer at least for me is not working – Adrian Cid Almaguer Dec 14 '18 at 19:52
  • @leymannx Try to search 'Français' that is a valid language for Drupal and you will not see the 'fr' code. – Adrian Cid Almaguer Dec 14 '18 at 19:56
  • The other answer is also wrong because it directly uses the class implementing a service, which is not something that should be done. – kiamlaluno Dec 18 '18 at 10:04
  • OK, I'm convinced. +1 :D – leymannx Dec 18 '18 at 11:09
2

Yes, the language_manager service can give you a list of available languages for which you can get the corresponding language ID, as said in the other answer.
Just don't access the LanguageManager class directly as that is the wrong way to use a service provided by Drupal or third-party modules.

Since you are letting the users select a language from a form, instead of using a textfield element, you should use a language_select element as in the following form builder code.

  $form['selected_langcode'] = [
    '#type' => 'language_select',
    '#title' => $this
      ->t('Language'),
    '#languages' => LanguageInterface::STATE_CONFIGURABLE | LanguageInterface::STATE_SITE_DEFAULT,
    '#default_value' => $config
      ->get('selected_langcode'),
  ];

Using that form element, the submission handler would get the language code for the language selected from the user with $form_state->getValue('selected_langcode').

For more details about the form element, you can read New language_select element in the Form API. For practical examples of uses of the form element, you can look at the uses of the language_select element.
In particular, you could be interested on how to show a full list of languages, instead of a list of languages set in the site. The following code would do that.

  $form['selected_langcode'] = [
    '#type' => 'language_select',
    '#title' => $this
      ->t('Language'),
    '#languages' => LanguageInterface::STATE_ALL,
    '#default_value' => $config
      ->get('selected_langcode'),
  ];

For a full list of available constants, see the documentation for LanguageInterface.

-2

Drupal 8 has a built-in function that lists all the language codes: LanguageManager::getStandardLanguageList(). My suggestion is to give the users a dropdown list of languages.

use Drupal\Core\Language\LanguageManager;
$standard_languages = LanguageManager::getStandardLanguageList();

foreach($standard_languages as $key => $val){
    if($val[0] == "English"){
        echo $key; //write your code

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.