summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-04-30 07:18:01 (GMT)
committerAlex Pott2014-04-30 07:18:01 (GMT)
commit4b172c72752df339a633e30aea1525c56be08192 (patch)
tree6ffabc6be91bdf95e21f090fd4496053b8108d62
parent4ad6fc9043ccdff61fa51c137da609805babd7bb (diff)
Issue #1978918 by likin, Luxian, vijaycs85, Pancho, disasm: Convert locale_translate_import_form to a Controller.
-rw-r--r--core/modules/locale/lib/Drupal/locale/Form/ImportForm.php197
-rw-r--r--core/modules/locale/lib/Drupal/locale/Form/LocaleForm.php10
-rw-r--r--core/modules/locale/locale.bulk.inc136
-rw-r--r--core/modules/locale/locale.routing.yml2
4 files changed, 198 insertions, 147 deletions
diff --git a/core/modules/locale/lib/Drupal/locale/Form/ImportForm.php b/core/modules/locale/lib/Drupal/locale/Form/ImportForm.php
new file mode 100644
index 0000000..7dc8e96
--- /dev/null
+++ b/core/modules/locale/lib/Drupal/locale/Form/ImportForm.php
@@ -0,0 +1,197 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\locale\Form\ImportForm.
+ */
+
+namespace Drupal\locale\Form;
+
+use Drupal\Core\Form\FormBase;
+use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
+use Drupal\Core\Extension\ModuleHandlerInterface;
+use Drupal\Core\Language\Language;
+use Drupal\Core\Language\LanguageManagerInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Form constructor for the translation import screen.
+ */
+class ImportForm extends FormBase implements ContainerInjectionInterface {
+
+ /**
+ * Uploaded file entity.
+ *
+ * @var \Drupal\file\Entity\File
+ */
+ protected $file;
+ /**
+ * The module handler service.
+ *
+ * @var \Drupal\Core\Extension\ModuleHandlerInterface
+ */
+ protected $moduleHandler;
+
+ /**
+ * The language manager.
+ *
+ * @var \Drupal\Core\Language\LanguageManagerInterface
+ */
+ protected $languageManager;
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('module_handler'),
+ $container->get('language_manager')
+ );
+ }
+ /**
+ * Constructs a form for language import.
+ *
+ * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
+ * The module handler service.
+ * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
+ * The language manager.
+ */
+ public function __construct(ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager) {
+ $this->moduleHandler = $module_handler;
+ $this->languageManager = $language_manager;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getFormID() {
+ return 'locale_translate_import_form';
+ }
+
+ /**
+ * Form constructor for the translation import screen.
+ *
+ * @ingroup forms
+ */
+ public function buildForm(array $form, array &$form_state) {
+ $languages = $this->languageManager->getLanguages();
+
+ // Initialize a language list to the ones available, including English if we
+ // are to translate Drupal to English as well.
+ $existing_languages = array();
+ foreach ($languages as $langcode => $language) {
+ if ($langcode != 'en' || locale_translate_english()) {
+ $existing_languages[$langcode] = $language->name;
+ }
+ }
+
+ // If we have no languages available, present the list of predefined
+ // languages only. If we do have already added languages, set up two option
+ // groups with the list of existing and then predefined languages.
+ form_load_include($form_state, 'inc', 'language', 'language.admin');
+ if (empty($existing_languages)) {
+ $language_options = language_admin_predefined_list();
+ $default = key($language_options);
+ }
+ else {
+ $default = key($existing_languages);
+ $language_options = array(
+ $this->t('Existing languages') => $existing_languages,
+ $this->t('Languages not yet added') => language_admin_predefined_list()
+ );
+ }
+
+ $validators = array(
+ 'file_validate_extensions' => array('po'),
+ 'file_validate_size' => array(file_upload_max_size()),
+ );
+ $form['file'] = array(
+ '#type' => 'file',
+ '#title' => $this->t('Translation file'),
+ '#description' => array(
+ '#theme' => 'file_upload_help',
+ '#description' => $this->t('A Gettext Portable Object file.'),
+ '#upload_validators' => $validators,
+ ),
+ '#size' => 50,
+ '#upload_validators' => $validators,
+ '#attributes' => array('class' => array('file-import-input')),
+ );
+ $form['langcode'] = array(
+ '#type' => 'select',
+ '#title' => $this->t('Language'),
+ '#options' => $language_options,
+ '#default_value' => $default,
+ '#attributes' => array('class' => array('langcode-input')),
+ );
+
+ $form['customized'] = array(
+ '#title' => $this->t('Treat imported strings as custom translations'),
+ '#type' => 'checkbox',
+ );
+ $form['overwrite_options'] = array(
+ '#type' => 'container',
+ '#tree' => TRUE,
+ );
+ $form['overwrite_options']['not_customized'] = array(
+ '#title' => $this->t('Overwrite non-customized translations'),
+ '#type' => 'checkbox',
+ '#states' => array(
+ 'checked' => array(
+ ':input[name="customized"]' => array('checked' => TRUE),
+ ),
+ ),
+ );
+ $form['overwrite_options']['customized'] = array(
+ '#title' => $this->t('Overwrite existing customized translations'),
+ '#type' => 'checkbox',
+ );
+
+ $form['actions'] = array(
+ '#type' => 'actions'
+ );
+ $form['actions']['submit'] = array(
+ '#type' => 'submit',
+ '#value' => $this->t('Import')
+ );
+ return $form;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function validateForm(array &$form, array &$form_state) {
+ $this->file = file_save_upload('file', $form['file']['#upload_validators'], 'translations://', 0);
+
+ // Ensure we have the file uploaded.
+ if (!$this->file) {
+ $this->setFormError('file', $form_state, $this->t('File to import not found.'));
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function submitForm(array &$form, array &$form_state) {
+ // Add language, if not yet supported.
+ $language = $this->languageManager->getLanguage($form_state['values']['langcode']);
+ if (empty($language)) {
+ $language = new Language(array(
+ 'id' => $form_state['values']['langcode']
+ ));
+ $language = language_save($language);
+ drupal_set_message($this->t('The language %language has been created.', array('%language' => $this->t($language->name))));
+ }
+ $options = array(
+ 'langcode' => $form_state['values']['langcode'],
+ 'overwrite_options' => $form_state['values']['overwrite_options'],
+ 'customized' => $form_state['values']['customized'] ? LOCALE_CUSTOMIZED : LOCALE_NOT_CUSTOMIZED,
+ );
+ $this->moduleHandler->loadInclude('locale', 'bulk.inc');
+ $file = locale_translate_file_attach_properties($this->file, $options);
+ $batch = locale_translate_batch_build(array($file->uri => $file), $options);
+ batch_set($batch);
+
+ $form_state['redirect_route']['route_name'] = 'locale.translate_page';
+ }
+}
diff --git a/core/modules/locale/lib/Drupal/locale/Form/LocaleForm.php b/core/modules/locale/lib/Drupal/locale/Form/LocaleForm.php
index 41857d0..280f1b0 100644
--- a/core/modules/locale/lib/Drupal/locale/Form/LocaleForm.php
+++ b/core/modules/locale/lib/Drupal/locale/Form/LocaleForm.php
@@ -12,16 +12,6 @@ namespace Drupal\locale\Form;
class LocaleForm {
/**
- * Wraps locale_translate_import_form().
- *
- * @todo Remove locale_translate_import_form().
- */
- public function import() {
- module_load_include('bulk.inc', 'locale');
- return \Drupal::formBuilder()->getForm('locale_translate_import_form');
- }
-
- /**
* Wraps locale_translation_status_form().
*
* @todo Remove locale_translation_status_form().
diff --git a/core/modules/locale/locale.bulk.inc b/core/modules/locale/locale.bulk.inc
index e93469f..f75a23e 100644
--- a/core/modules/locale/locale.bulk.inc
+++ b/core/modules/locale/locale.bulk.inc
@@ -10,142 +10,6 @@ use Drupal\Core\Language\Language;
use Drupal\file\FileInterface;
/**
- * Form constructor for the translation import screen.
- *
- * @see locale_translate_import_form_submit()
- * @ingroup forms
- *
- * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
- * Use \Drupal\locale\Form\LocaleForm::import().
- */
-function locale_translate_import_form($form, &$form_state) {
- Drupal::languageManager()->reset();
- $languages = \Drupal::languageManager()->getLanguages();
-
- // Initialize a language list to the ones available, including English if we
- // are to translate Drupal to English as well.
- $existing_languages = array();
- foreach ($languages as $langcode => $language) {
- if ($langcode != 'en' || locale_translate_english()) {
- $existing_languages[$langcode] = $language->name;
- }
- }
-
- // If we have no languages available, present the list of predefined languages
- // only. If we do have already added languages, set up two option groups with
- // the list of existing and then predefined languages.
- form_load_include($form_state, 'inc', 'language', 'language.admin');
- if (empty($existing_languages)) {
- $language_options = language_admin_predefined_list();
- $default = key($language_options);
- }
- else {
- $default = key($existing_languages);
- $language_options = array(
- t('Existing languages') => $existing_languages,
- t('Languages not yet added') => language_admin_predefined_list()
- );
- }
-
- $validators = array(
- 'file_validate_extensions' => array('po'),
- 'file_validate_size' => array(file_upload_max_size()),
- );
- $file_description = array(
- '#theme' => 'file_upload_help',
- '#description' => t('A Gettext Portable Object file.'),
- '#upload_validators' => $validators,
- );
-
- $form['file'] = array(
- '#type' => 'file',
- '#title' => t('Translation file'),
- '#description' => drupal_render($file_description),
- '#size' => 50,
- '#upload_validators' => $validators,
- '#attributes' => array('class' => array('file-import-input')),
- '#attached' => array(
- 'js' => array(
- drupal_get_path('module', 'locale') . '/locale.bulk.js' => array(),
- ),
- ),
- );
- $form['langcode'] = array(
- '#type' => 'select',
- '#title' => t('Language'),
- '#options' => $language_options,
- '#default_value' => $default,
- '#attributes' => array('class' => array('langcode-input')),
- );
-
- $form['customized'] = array(
- '#title' => t('Treat imported strings as custom translations'),
- '#type' => 'checkbox',
- );
- $form['overwrite_options'] = array(
- '#type' => 'container',
- '#tree' => TRUE,
- );
- $form['overwrite_options']['not_customized'] = array(
- '#title' => t('Overwrite non-customized translations'),
- '#type' => 'checkbox',
- '#states' => array(
- 'checked' => array(
- ':input[name="customized"]' => array('checked' => TRUE),
- ),
- ),
- );
- $form['overwrite_options']['customized'] = array(
- '#title' => t('Overwrite existing customized translations'),
- '#type' => 'checkbox',
- );
-
- $form['actions'] = array(
- '#type' => 'actions'
- );
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Import')
- );
- return $form;
-}
-
-/**
- * Form submission handler for locale_translate_import_form().
- */
-function locale_translate_import_form_submit($form, &$form_state) {
- // Ensure we have the file uploaded.
- if ($file = file_save_upload('file', $form['file']['#upload_validators'], 'translations://', 0)) {
-
- // Add language, if not yet supported.
- $language = \Drupal::languageManager()->getLanguage($form_state['values']['langcode']);
- if (empty($language)) {
- $language = new Language(array(
- 'id' => $form_state['values']['langcode']
- ));
- $language = language_save($language);
- drupal_set_message(t('The language %language has been created.', array('%language' => t($language->name))));
- }
- $options = array(
- 'langcode' => $form_state['values']['langcode'],
- 'overwrite_options' => $form_state['values']['overwrite_options'],
- 'customized' => $form_state['values']['customized'] ? LOCALE_CUSTOMIZED : LOCALE_NOT_CUSTOMIZED,
- );
- $file = locale_translate_file_attach_properties($file, $options);
- $batch = locale_translate_batch_build(array($file->uri => $file), $options);
- batch_set($batch);
- }
- else {
- drupal_set_message(t('File to import not found.'), 'error');
- $form_state['rebuild'] = TRUE;
- return;
- }
-
- $form_state['redirect_route']['route_name'] = 'locale.translate_page';
- return;
-}
-
-/**
* Prepare a batch to import all translations.
*
* @param array $options
diff --git a/core/modules/locale/locale.routing.yml b/core/modules/locale/locale.routing.yml
index 6658dad..705d5ea 100644
--- a/core/modules/locale/locale.routing.yml
+++ b/core/modules/locale/locale.routing.yml
@@ -24,7 +24,7 @@ locale.translate_page:
locale.translate_import:
path: '/admin/config/regional/translate/import'
defaults:
- _content: '\Drupal\locale\Form\LocaleForm::import'
+ _form: '\Drupal\locale\Form\ImportForm'
_title: 'Import'
requirements:
_permission: 'translate interface'