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