Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\language\Element;
|
Chris@0
|
4
|
Chris@18
|
5 use Drupal\Core\Url;
|
Chris@0
|
6 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
7 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
8 use Drupal\Core\Render\Element\FormElement;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Defines an element for language configuration for a single field.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @FormElement("language_configuration")
|
Chris@0
|
14 */
|
Chris@0
|
15 class LanguageConfiguration extends FormElement {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * {@inheritdoc}
|
Chris@0
|
19 */
|
Chris@0
|
20 public function getInfo() {
|
Chris@0
|
21 $class = get_class($this);
|
Chris@0
|
22 return [
|
Chris@0
|
23 '#input' => TRUE,
|
Chris@0
|
24 '#tree' => TRUE,
|
Chris@0
|
25 '#process' => [
|
Chris@0
|
26 [$class, 'processLanguageConfiguration'],
|
Chris@0
|
27 ],
|
Chris@0
|
28 ];
|
Chris@0
|
29 }
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Process handler for the language_configuration form element.
|
Chris@0
|
33 */
|
Chris@0
|
34 public static function processLanguageConfiguration(&$element, FormStateInterface $form_state, &$form) {
|
Chris@0
|
35 $options = isset($element['#options']) ? $element['#options'] : [];
|
Chris@0
|
36 // Avoid validation failure since we are moving the '#options' key in the
|
Chris@0
|
37 // nested 'language' select element.
|
Chris@0
|
38 unset($element['#options']);
|
Chris@0
|
39 /** @var \Drupal\language\Entity\ContentLanguageSettings $default_config */
|
Chris@0
|
40 $default_config = $element['#default_value'];
|
Chris@0
|
41 $element['langcode'] = [
|
Chris@0
|
42 '#type' => 'select',
|
Chris@0
|
43 '#title' => t('Default language'),
|
Chris@0
|
44 '#options' => $options + static::getDefaultOptions(),
|
Chris@18
|
45 '#description' => t('Explanation of the language options is found on the <a href=":languages_list_page">languages list page</a>.', [':languages_list_page' => Url::fromRoute('entity.configurable_language.collection')->toString()]),
|
Chris@0
|
46 '#default_value' => ($default_config != NULL) ? $default_config->getDefaultLangcode() : LanguageInterface::LANGCODE_SITE_DEFAULT,
|
Chris@0
|
47 ];
|
Chris@0
|
48
|
Chris@0
|
49 $element['language_alterable'] = [
|
Chris@0
|
50 '#type' => 'checkbox',
|
Chris@0
|
51 '#title' => t('Show language selector on create and edit pages'),
|
Chris@0
|
52 '#default_value' => ($default_config != NULL) ? $default_config->isLanguageAlterable() : FALSE,
|
Chris@0
|
53 ];
|
Chris@0
|
54
|
Chris@0
|
55 // Add the entity type and bundle information to the form if they are set.
|
Chris@0
|
56 // They will be used, in the submit handler, to generate the names of the
|
Chris@0
|
57 // configuration entities that will store the settings and are a way to uniquely
|
Chris@0
|
58 // identify the entity.
|
Chris@0
|
59 $language = $form_state->get('language') ?: [];
|
Chris@0
|
60 $language += [
|
Chris@0
|
61 $element['#name'] => [
|
Chris@0
|
62 'entity_type' => $element['#entity_information']['entity_type'],
|
Chris@0
|
63 'bundle' => $element['#entity_information']['bundle'],
|
Chris@0
|
64 ],
|
Chris@0
|
65 ];
|
Chris@0
|
66 $form_state->set('language', $language);
|
Chris@0
|
67
|
Chris@0
|
68 // Do not add the submit callback for the language content settings page,
|
Chris@0
|
69 // which is handled separately.
|
Chris@0
|
70 if ($form['#form_id'] != 'language_content_settings_form') {
|
Chris@0
|
71 // Determine where to attach the language_configuration element submit
|
Chris@0
|
72 // handler.
|
Chris@0
|
73 // @todo Form API: Allow form widgets/sections to declare #submit
|
Chris@0
|
74 // handlers.
|
Chris@0
|
75 $submit_name = isset($form['actions']['save_continue']) ? 'save_continue' : 'submit';
|
Chris@0
|
76 if (isset($form['actions'][$submit_name]['#submit']) && array_search('language_configuration_element_submit', $form['actions'][$submit_name]['#submit']) === FALSE) {
|
Chris@0
|
77 $form['actions'][$submit_name]['#submit'][] = 'language_configuration_element_submit';
|
Chris@0
|
78 }
|
Chris@0
|
79 elseif (array_search('language_configuration_element_submit', $form['#submit']) === FALSE) {
|
Chris@0
|
80 $form['#submit'][] = 'language_configuration_element_submit';
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|
Chris@0
|
83 return $element;
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Returns the default options for the language configuration form element.
|
Chris@0
|
88 *
|
Chris@0
|
89 * @return array
|
Chris@0
|
90 * An array containing the default options.
|
Chris@0
|
91 */
|
Chris@0
|
92 protected static function getDefaultOptions() {
|
Chris@0
|
93 $language_options = [
|
Chris@0
|
94 LanguageInterface::LANGCODE_SITE_DEFAULT => t("Site's default language (@language)", ['@language' => static::languageManager()->getDefaultLanguage()->getName()]),
|
Chris@0
|
95 'current_interface' => t('Interface text language selected for page'),
|
Chris@0
|
96 'authors_default' => t("Author's preferred language"),
|
Chris@0
|
97 ];
|
Chris@0
|
98
|
Chris@0
|
99 $languages = static::languageManager()->getLanguages(LanguageInterface::STATE_ALL);
|
Chris@0
|
100 foreach ($languages as $langcode => $language) {
|
Chris@0
|
101 $language_options[$langcode] = $language->isLocked() ? t('- @name -', ['@name' => $language->getName()]) : $language->getName();
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 return $language_options;
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 /**
|
Chris@0
|
108 * Wraps the language manager.
|
Chris@0
|
109 *
|
Chris@0
|
110 * @return \Drupal\Core\Language\LanguageManagerInterface
|
Chris@0
|
111 */
|
Chris@0
|
112 protected static function languageManager() {
|
Chris@0
|
113 return \Drupal::languageManager();
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 }
|