ExportForm::submitForm

public ExportForm::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/ExportForm.php, line 126

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', array());
  $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 = 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 {
    drupal_set_message($this->t('Nothing to export.'));
  }
}
doc_Drupal
2016-10-29 09:09:35
Comments
Leave a Comment

Please login to continue.