You are here

public function ExportForm::submitForm in Drupal 8

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

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/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.'));
  }
}