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