Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / locale / src / Form / ImportForm.php
1 <?php
2
3 namespace Drupal\locale\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\language\ConfigurableLanguageManagerInterface;
9 use Drupal\language\Entity\ConfigurableLanguage;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Form constructor for the translation import screen.
14  */
15 class ImportForm extends FormBase {
16
17   /**
18    * Uploaded file entity.
19    *
20    * @var \Drupal\file\Entity\File
21    */
22   protected $file;
23
24   /**
25    * The module handler service.
26    *
27    * @var \Drupal\Core\Extension\ModuleHandlerInterface
28    */
29   protected $moduleHandler;
30
31   /**
32    * The configurable language manager.
33    *
34    * @var \Drupal\language\ConfigurableLanguageManagerInterface
35    */
36   protected $languageManager;
37
38   /**
39    * {@inheritdoc}
40    */
41   public static function create(ContainerInterface $container) {
42     return new static(
43       $container->get('module_handler'),
44       $container->get('language_manager')
45     );
46   }
47   /**
48    * Constructs a form for language import.
49    *
50    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
51    *   The module handler service.
52    * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
53    *   The configurable language manager.
54    */
55   public function __construct(ModuleHandlerInterface $module_handler, ConfigurableLanguageManagerInterface $language_manager) {
56     $this->moduleHandler = $module_handler;
57     $this->languageManager = $language_manager;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function getFormId() {
64     return 'locale_translate_import_form';
65   }
66
67   /**
68    * Form constructor for the translation import screen.
69    */
70   public function buildForm(array $form, FormStateInterface $form_state) {
71     $languages = $this->languageManager->getLanguages();
72
73     // Initialize a language list to the ones available, including English if we
74     // are to translate Drupal to English as well.
75     $existing_languages = [];
76     foreach ($languages as $langcode => $language) {
77       if (locale_is_translatable($langcode)) {
78         $existing_languages[$langcode] = $language->getName();
79       }
80     }
81
82     // If we have no languages available, present the list of predefined
83     // languages only. If we do have already added languages, set up two option
84     // groups with the list of existing and then predefined languages.
85     if (empty($existing_languages)) {
86       $language_options = $this->languageManager->getStandardLanguageListWithoutConfigured();
87       $default = key($language_options);
88     }
89     else {
90       $default = key($existing_languages);
91       $language_options = [
92         (string) $this->t('Existing languages') => $existing_languages,
93         (string) $this->t('Languages not yet added') => $this->languageManager->getStandardLanguageListWithoutConfigured(),
94       ];
95     }
96
97     $validators = [
98       'file_validate_extensions' => ['po'],
99       'file_validate_size' => [file_upload_max_size()],
100     ];
101     $form['file'] = [
102       '#type' => 'file',
103       '#title' => $this->t('Translation file'),
104       '#description' => [
105         '#theme' => 'file_upload_help',
106         '#description' => $this->t('A Gettext Portable Object file.'),
107         '#upload_validators' => $validators,
108       ],
109       '#size' => 50,
110       '#upload_validators' => $validators,
111       '#attributes' => ['class' => ['file-import-input']],
112     ];
113     $form['langcode'] = [
114       '#type' => 'select',
115       '#title' => $this->t('Language'),
116       '#options' => $language_options,
117       '#default_value' => $default,
118       '#attributes' => ['class' => ['langcode-input']],
119     ];
120
121     $form['customized'] = [
122       '#title' => $this->t('Treat imported strings as custom translations'),
123       '#type' => 'checkbox',
124     ];
125     $form['overwrite_options'] = [
126       '#type' => 'container',
127       '#tree' => TRUE,
128     ];
129     $form['overwrite_options']['not_customized'] = [
130       '#title' => $this->t('Overwrite non-customized translations'),
131       '#type' => 'checkbox',
132       '#states' => [
133         'checked' => [
134           ':input[name="customized"]' => ['checked' => TRUE],
135         ],
136       ],
137     ];
138     $form['overwrite_options']['customized'] = [
139       '#title' => $this->t('Overwrite existing customized translations'),
140       '#type' => 'checkbox',
141     ];
142
143     $form['actions'] = [
144       '#type' => 'actions',
145     ];
146     $form['actions']['submit'] = [
147       '#type' => 'submit',
148       '#value' => $this->t('Import'),
149     ];
150     return $form;
151   }
152
153   /**
154    * {@inheritdoc}
155    */
156   public function validateForm(array &$form, FormStateInterface $form_state) {
157     $this->file = file_save_upload('file', $form['file']['#upload_validators'], 'translations://', 0);
158
159     // Ensure we have the file uploaded.
160     if (!$this->file) {
161       $form_state->setErrorByName('file', $this->t('File to import not found.'));
162     }
163   }
164
165   /**
166    * {@inheritdoc}
167    */
168   public function submitForm(array &$form, FormStateInterface $form_state) {
169     $this->moduleHandler->loadInclude('locale', 'translation.inc');
170     // Add language, if not yet supported.
171     $language = $this->languageManager->getLanguage($form_state->getValue('langcode'));
172     if (empty($language)) {
173       $language = ConfigurableLanguage::createFromLangcode($form_state->getValue('langcode'));
174       $language->save();
175       drupal_set_message($this->t('The language %language has been created.', ['%language' => $this->t($language->label())]));
176     }
177     $options = array_merge(_locale_translation_default_update_options(), [
178       'langcode' => $form_state->getValue('langcode'),
179       'overwrite_options' => $form_state->getValue('overwrite_options'),
180       'customized' => $form_state->getValue('customized') ? LOCALE_CUSTOMIZED : LOCALE_NOT_CUSTOMIZED,
181     ]);
182     $this->moduleHandler->loadInclude('locale', 'bulk.inc');
183     $file = locale_translate_file_attach_properties($this->file, $options);
184     $batch = locale_translate_batch_build([$file->uri => $file], $options);
185     batch_set($batch);
186
187     // Create or update all configuration translations for this language.
188     if ($batch = locale_config_batch_update_components($options, [$form_state->getValue('langcode')])) {
189       batch_set($batch);
190     }
191
192     $form_state->setRedirect('locale.translate_page');
193   }
194
195 }