Chris@0: TRUE, Chris@0: '#tree' => TRUE, Chris@0: '#process' => [ Chris@0: [$class, 'processLanguageConfiguration'], Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Process handler for the language_configuration form element. Chris@0: */ Chris@0: public static function processLanguageConfiguration(&$element, FormStateInterface $form_state, &$form) { Chris@0: $options = isset($element['#options']) ? $element['#options'] : []; Chris@0: // Avoid validation failure since we are moving the '#options' key in the Chris@0: // nested 'language' select element. Chris@0: unset($element['#options']); Chris@0: /** @var \Drupal\language\Entity\ContentLanguageSettings $default_config */ Chris@0: $default_config = $element['#default_value']; Chris@0: $element['langcode'] = [ Chris@0: '#type' => 'select', Chris@0: '#title' => t('Default language'), Chris@0: '#options' => $options + static::getDefaultOptions(), Chris@18: '#description' => t('Explanation of the language options is found on the languages list page.', [':languages_list_page' => Url::fromRoute('entity.configurable_language.collection')->toString()]), Chris@0: '#default_value' => ($default_config != NULL) ? $default_config->getDefaultLangcode() : LanguageInterface::LANGCODE_SITE_DEFAULT, Chris@0: ]; Chris@0: Chris@0: $element['language_alterable'] = [ Chris@0: '#type' => 'checkbox', Chris@0: '#title' => t('Show language selector on create and edit pages'), Chris@0: '#default_value' => ($default_config != NULL) ? $default_config->isLanguageAlterable() : FALSE, Chris@0: ]; Chris@0: Chris@0: // Add the entity type and bundle information to the form if they are set. Chris@0: // They will be used, in the submit handler, to generate the names of the Chris@0: // configuration entities that will store the settings and are a way to uniquely Chris@0: // identify the entity. Chris@0: $language = $form_state->get('language') ?: []; Chris@0: $language += [ Chris@0: $element['#name'] => [ Chris@0: 'entity_type' => $element['#entity_information']['entity_type'], Chris@0: 'bundle' => $element['#entity_information']['bundle'], Chris@0: ], Chris@0: ]; Chris@0: $form_state->set('language', $language); Chris@0: Chris@0: // Do not add the submit callback for the language content settings page, Chris@0: // which is handled separately. Chris@0: if ($form['#form_id'] != 'language_content_settings_form') { Chris@0: // Determine where to attach the language_configuration element submit Chris@0: // handler. Chris@0: // @todo Form API: Allow form widgets/sections to declare #submit Chris@0: // handlers. Chris@0: $submit_name = isset($form['actions']['save_continue']) ? 'save_continue' : 'submit'; Chris@0: if (isset($form['actions'][$submit_name]['#submit']) && array_search('language_configuration_element_submit', $form['actions'][$submit_name]['#submit']) === FALSE) { Chris@0: $form['actions'][$submit_name]['#submit'][] = 'language_configuration_element_submit'; Chris@0: } Chris@0: elseif (array_search('language_configuration_element_submit', $form['#submit']) === FALSE) { Chris@0: $form['#submit'][] = 'language_configuration_element_submit'; Chris@0: } Chris@0: } Chris@0: return $element; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the default options for the language configuration form element. Chris@0: * Chris@0: * @return array Chris@0: * An array containing the default options. Chris@0: */ Chris@0: protected static function getDefaultOptions() { Chris@0: $language_options = [ Chris@0: LanguageInterface::LANGCODE_SITE_DEFAULT => t("Site's default language (@language)", ['@language' => static::languageManager()->getDefaultLanguage()->getName()]), Chris@0: 'current_interface' => t('Interface text language selected for page'), Chris@0: 'authors_default' => t("Author's preferred language"), Chris@0: ]; Chris@0: Chris@0: $languages = static::languageManager()->getLanguages(LanguageInterface::STATE_ALL); Chris@0: foreach ($languages as $langcode => $language) { Chris@0: $language_options[$langcode] = $language->isLocked() ? t('- @name -', ['@name' => $language->getName()]) : $language->getName(); Chris@0: } Chris@0: Chris@0: return $language_options; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Wraps the language manager. Chris@0: * Chris@0: * @return \Drupal\Core\Language\LanguageManagerInterface Chris@0: */ Chris@0: protected static function languageManager() { Chris@0: return \Drupal::languageManager(); Chris@0: } Chris@0: Chris@0: }