You are here

public function WebformDevelEntityFormApiExportForm::submitForm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_devel/src/Form/WebformDevelEntityFormApiExportForm.php \Drupal\webform_devel\Form\WebformDevelEntityFormApiExportForm::submitForm()

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

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 EntityForm::submitForm

File

modules/webform_devel/src/Form/WebformDevelEntityFormApiExportForm.php, line 162

Class

WebformDevelEntityFormApiExportForm
Export a webform's element to Form API (FAPI).

Namespace

Drupal\webform_devel\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = $this
    ->getEntity();

  // Get the Tar archive.
  $archive_file_path = \Drupal::service('file_system')
    ->getTempDirectory() . '/' . $webform
    ->id() . '.tar.gz';
  $archive = new \Archive_Tar($archive_file_path, 'gz');

  // Add code to archive.
  $file_names = $form['code']['#file_names'];
  $code = $form_state
    ->getValue('code');
  foreach ($file_names as $key => $file_name) {
    $archive
      ->addString($file_name, $code[$key]);
  }

  // Set archive as the response and delete the temp file.
  $response = new BinaryFileResponse($archive_file_path, 200, [], FALSE);
  $response
    ->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $webform
    ->id() . '.tar.gz');
  $response
    ->deleteFileAfterSend(TRUE);
  $form_state
    ->setResponse($response);
}