Skip to main content
made a sentence clearer
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

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 because a module could have replacedas that is the language_manager service implementation with an implementation done from a different class. If you werewrong way to use the LanguageManager class directly, you would use the wrong classa 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.

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 because a module could have replaced the language_manager service implementation with an implementation done from a different class. If you were to use the LanguageManager class directly, you would use the wrong class.

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.

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.

Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

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 because a module could have replaced the language_manager service implementation with an implementation done from a different class. If you were to use the LanguageManager class directly, you would use the wrong class.

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.