TranslateEditForm::submitForm

public TranslateEditForm::submitForm(array &$form, FormStateInterface $form_state)

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

core/modules/locale/src/Form/TranslateEditForm.php, line 170

Class

TranslateEditForm
Defines a translation edit form.

Namespace

Drupal\locale\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $langcode = $form_state->getValue('langcode');
  $updated = array();

  // Preload all translations for strings in the form.
  $lids = array_keys($form_state->getValue('strings'));
  $existing_translation_objects = array();
  foreach ($this->localeStorage->getTranslations(array('lid' => $lids, 'language' => $langcode, 'translated' => TRUE)) as $existing_translation_object) {
    $existing_translation_objects[$existing_translation_object->lid] = $existing_translation_object;
  }

  foreach ($form_state->getValue('strings') as $lid => $new_translation) {
    $existing_translation = isset($existing_translation_objects[$lid]);

    // Plural translations are saved in a delimited string. To be able to
    // compare the new strings with the existing strings a string in the same
    // format is created.
    $new_translation_string_delimited = implode(LOCALE_PLURAL_DELIMITER, $new_translation['translations']);

    // Generate an imploded string without delimiter, to be able to run
    // empty() on it.
    $new_translation_string = implode('', $new_translation['translations']);

    $is_changed = FALSE;

    if ($existing_translation && $existing_translation_objects[$lid]->translation != $new_translation_string_delimited) {
      // If there is an existing translation in the DB and the new translation
      // is not the same as the existing one.
      $is_changed = TRUE;
    }
    elseif (!$existing_translation && !empty($new_translation_string)) {
      // Newly entered translation.
      $is_changed = TRUE;
    }

    if ($is_changed) {
      // Only update or insert if we have a value to use.
      $target = isset($existing_translation_objects[$lid]) ? $existing_translation_objects[$lid] : $this->localeStorage->createTranslation(array('lid' => $lid, 'language' => $langcode));
      $target->setPlurals($new_translation['translations'])
        ->setCustomized()
        ->save();
      $updated[] = $target->getId();
    }
    if (empty($new_translation_string) && isset($existing_translation_objects[$lid])) {
      // Empty new translation entered: remove existing entry from database.
      $existing_translation_objects[$lid]->delete();
      $updated[] = $lid;
    }
  }

  drupal_set_message($this->t('The strings have been saved.'));

  // Keep the user on the current pager page.
  $page = $this->getRequest()->query->get('page');
  if (isset($page)) {
    $form_state->setRedirect(
    'locale.translate_page', 
    array(), 
    array('page' => $page)
    );
  }

  if ($updated) {
    // Clear cache and force refresh of JavaScript translations.
    _locale_refresh_translations(array($langcode), $updated);
    _locale_refresh_configuration(array($langcode), $updated);
  }
}
doc_Drupal
2016-10-29 09:49:07
Comments
Leave a Comment

Please login to continue.