function TranslateEditForm::submitForm

Same name and namespace in other branches
  1. 9 core/modules/locale/src/Form/TranslateEditForm.php \Drupal\locale\Form\TranslateEditForm::submitForm()
  2. 8.9.x core/modules/locale/src/Form/TranslateEditForm.php \Drupal\locale\Form\TranslateEditForm::submitForm()
  3. 10 core/modules/locale/src/Form/TranslateEditForm.php \Drupal\locale\Form\TranslateEditForm::submitForm()

Overrides FormInterface::submitForm

File

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

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 = [];
    // Preload all translations for strings in the form.
    $lids = array_keys($form_state->getValue('strings'));
    $existing_translation_objects = [];
    foreach ($this->localeStorage
        ->getTranslations([
        '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(PoItem::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 = $existing_translation_objects[$lid] ?? $this->localeStorage
                ->createTranslation([
                '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;
        }
    }
    $this->messenger()
        ->addStatus($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', [], [
            'page' => $page,
        ]);
    }
    if ($updated) {
        // Clear cache and force refresh of JavaScript translations.
        _locale_refresh_translations([
            $langcode,
        ], $updated);
        _locale_refresh_configuration([
            $langcode,
        ], $updated);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.