function ExportForm::submitForm

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

Overrides FormInterface::submitForm

File

core/modules/locale/src/Form/ExportForm.php, line 139

Class

ExportForm
Form for the Gettext translation files export form.

Namespace

Drupal\locale\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
    // If template is required, language code is not given.
    if ($form_state->getValue('langcode') != LanguageInterface::LANGCODE_SYSTEM) {
        $language = $this->languageManager
            ->getLanguage($form_state->getValue('langcode'));
    }
    else {
        $language = NULL;
    }
    $content_options = $form_state->getValue('content_options', []);
    $reader = new PoDatabaseReader();
    $language_name = '';
    if ($language != NULL) {
        $reader->setLangcode($language->getId());
        $reader->setOptions($content_options);
        $languages = $this->languageManager
            ->getLanguages();
        $language_name = isset($languages[$language->getId()]) ? $languages[$language->getId()]
            ->getName() : '';
        $filename = $language->getId() . '.po';
    }
    else {
        // Template required.
        $filename = 'drupal.pot';
    }
    $item = $reader->readItem();
    if (!empty($item)) {
        $uri = $this->fileSystem
            ->tempnam('temporary://', 'po_');
        $header = $reader->getHeader();
        $header->setProjectName($this->config('system.site')
            ->get('name'));
        $header->setLanguageName($language_name);
        $writer = new PoStreamWriter();
        $writer->setURI($uri);
        $writer->setHeader($header);
        $writer->open();
        $writer->writeItem($item);
        $writer->writeItems($reader);
        $writer->close();
        $response = new BinaryFileResponse($uri);
        $response->setContentDisposition('attachment', $filename);
        $form_state->setResponse($response);
    }
    else {
        $this->messenger()
            ->addStatus($this->t('Nothing to export.'));
    }
}

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