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
  • 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
    Commented Dec 18, 2018 at 10:22

3 Answers 3

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);

Or, if you are on PHP 7.4+:

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

$languages = \Drupal::service('language_manager')::getStandardLanguageList();
$langcode = key(array_filter($languages, fn ($lang) => in_array($find, $lang)));
7
  • TBH, this adds nothing new to the already given answer...
    – leymannx
    Commented Dec 14, 2018 at 19:44
  • @leymannx the code in the other answer at least for me is not working Commented Dec 14, 2018 at 19:52
  • @leymannx Try to search 'Français' that is a valid language for Drupal and you will not see the 'fr' code. Commented Dec 14, 2018 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.
    – avpaderno
    Commented Dec 18, 2018 at 10:04
  • 1
    OK, I'm convinced. +1 :D
    – leymannx
    Commented Dec 18, 2018 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 and acknowledge you have read our privacy policy.

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