Mercurial > hg > isophonics-drupal-site
comparison core/modules/language/src/Element/LanguageConfiguration.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\language\Element; | |
4 | |
5 use Drupal\Core\Form\FormStateInterface; | |
6 use Drupal\Core\Language\LanguageInterface; | |
7 use Drupal\Core\Render\Element\FormElement; | |
8 | |
9 /** | |
10 * Defines an element for language configuration for a single field. | |
11 * | |
12 * @FormElement("language_configuration") | |
13 */ | |
14 class LanguageConfiguration extends FormElement { | |
15 | |
16 /** | |
17 * {@inheritdoc} | |
18 */ | |
19 public function getInfo() { | |
20 $class = get_class($this); | |
21 return [ | |
22 '#input' => TRUE, | |
23 '#tree' => TRUE, | |
24 '#process' => [ | |
25 [$class, 'processLanguageConfiguration'], | |
26 ], | |
27 ]; | |
28 } | |
29 | |
30 /** | |
31 * Process handler for the language_configuration form element. | |
32 */ | |
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'] = [ | |
41 '#type' => 'select', | |
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, | |
46 ]; | |
47 | |
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, | |
52 ]; | |
53 | |
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') ?: []; | |
59 $language += [ | |
60 $element['#name'] => [ | |
61 'entity_type' => $element['#entity_information']['entity_type'], | |
62 'bundle' => $element['#entity_information']['bundle'], | |
63 ], | |
64 ]; | |
65 $form_state->set('language', $language); | |
66 | |
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 | |
71 // handler. | |
72 // @todo Form API: Allow form widgets/sections to declare #submit | |
73 // handlers. | |
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'; | |
77 } | |
78 elseif (array_search('language_configuration_element_submit', $form['#submit']) === FALSE) { | |
79 $form['#submit'][] = 'language_configuration_element_submit'; | |
80 } | |
81 } | |
82 return $element; | |
83 } | |
84 | |
85 /** | |
86 * Returns the default options for the language configuration form element. | |
87 * | |
88 * @return array | |
89 * An array containing the default options. | |
90 */ | |
91 protected static function getDefaultOptions() { | |
92 $language_options = [ | |
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"), | |
96 ]; | |
97 | |
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(); | |
101 } | |
102 | |
103 return $language_options; | |
104 } | |
105 | |
106 /** | |
107 * Wraps the language manager. | |
108 * | |
109 * @return \Drupal\Core\Language\LanguageManagerInterface | |
110 */ | |
111 protected static function languageManager() { | |
112 return \Drupal::languageManager(); | |
113 } | |
114 | |
115 } |