3 namespace Drupal\language\Element;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Core\Render\Element\FormElement;
10 * Defines an element for language configuration for a single field.
12 * @FormElement("language_configuration")
14 class LanguageConfiguration extends FormElement {
19 public function getInfo() {
20 $class = get_class($this);
25 [$class, 'processLanguageConfiguration'],
31 * Process handler for the language_configuration form element.
33 public static function processLanguageConfiguration(&$element, FormStateInterface $form_state, &$form) {
34 $options = isset($element['#options']) ? $element['#options'] : [];
35 // Avoid validation failure since we are moving the '#options' key in the
36 // nested 'language' select element.
37 unset($element['#options']);
38 /** @var \Drupal\language\Entity\ContentLanguageSettings $default_config */
39 $default_config = $element['#default_value'];
40 $element['langcode'] = [
42 '#title' => t('Default language'),
43 '#options' => $options + static::getDefaultOptions(),
44 '#description' => t('Explanation of the language options is found on the <a href=":languages_list_page">languages list page</a>.', [':languages_list_page' => \Drupal::url('entity.configurable_language.collection')]),
45 '#default_value' => ($default_config != NULL) ? $default_config->getDefaultLangcode() : LanguageInterface::LANGCODE_SITE_DEFAULT,
48 $element['language_alterable'] = [
49 '#type' => 'checkbox',
50 '#title' => t('Show language selector on create and edit pages'),
51 '#default_value' => ($default_config != NULL) ? $default_config->isLanguageAlterable() : FALSE,
54 // Add the entity type and bundle information to the form if they are set.
55 // They will be used, in the submit handler, to generate the names of the
56 // configuration entities that will store the settings and are a way to uniquely
57 // identify the entity.
58 $language = $form_state->get('language') ?: [];
60 $element['#name'] => [
61 'entity_type' => $element['#entity_information']['entity_type'],
62 'bundle' => $element['#entity_information']['bundle'],
65 $form_state->set('language', $language);
67 // Do not add the submit callback for the language content settings page,
68 // which is handled separately.
69 if ($form['#form_id'] != 'language_content_settings_form') {
70 // Determine where to attach the language_configuration element submit
72 // @todo Form API: Allow form widgets/sections to declare #submit
74 $submit_name = isset($form['actions']['save_continue']) ? 'save_continue' : 'submit';
75 if (isset($form['actions'][$submit_name]['#submit']) && array_search('language_configuration_element_submit', $form['actions'][$submit_name]['#submit']) === FALSE) {
76 $form['actions'][$submit_name]['#submit'][] = 'language_configuration_element_submit';
78 elseif (array_search('language_configuration_element_submit', $form['#submit']) === FALSE) {
79 $form['#submit'][] = 'language_configuration_element_submit';
86 * Returns the default options for the language configuration form element.
89 * An array containing the default options.
91 protected static function getDefaultOptions() {
93 LanguageInterface::LANGCODE_SITE_DEFAULT => t("Site's default language (@language)", ['@language' => static::languageManager()->getDefaultLanguage()->getName()]),
94 'current_interface' => t('Interface text language selected for page'),
95 'authors_default' => t("Author's preferred language"),
98 $languages = static::languageManager()->getLanguages(LanguageInterface::STATE_ALL);
99 foreach ($languages as $langcode => $language) {
100 $language_options[$langcode] = $language->isLocked() ? t('- @name -', ['@name' => $language->getName()]) : $language->getName();
103 return $language_options;
107 * Wraps the language manager.
109 * @return \Drupal\Core\Language\LanguageManagerInterface
111 protected static function languageManager() {
112 return \Drupal::languageManager();