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