You are here

public function WebformDevelSchemaController::index in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_devel/src/Controller/WebformDevelSchemaController.php \Drupal\webform_devel\Controller\WebformDevelSchemaController::index()

Returns a webform's schema as a CSV.

Parameters

\Drupal\webform\WebformInterface $webform: The webform to be exported.

Return value

\Symfony\Component\HttpFoundation\StreamedResponse A streamed response containing webform's schema as a CSV.

1 string reference to 'WebformDevelSchemaController::index'
webform_devel.routing.yml in modules/webform_devel/webform_devel.routing.yml
modules/webform_devel/webform_devel.routing.yml

File

modules/webform_devel/src/Controller/WebformDevelSchemaController.php, line 49

Class

WebformDevelSchemaController
Provides route responses for webform devel schema.

Namespace

Drupal\webform_devel\Controller

Code

public function index(WebformInterface $webform) {
  $multiple_delimiter = $this->configFactory
    ->get('webform.settings')
    ->get('export.multiple_delimiter') ?: ';';

  // From: http://obtao.com/blog/2013/12/export-data-to-a-csv-file-with-symfony/
  $response = new StreamedResponse(function () use ($webform, $multiple_delimiter) {
    $handle = fopen('php://output', 'r+');

    // Header.
    fputcsv($handle, $this->schema
      ->getColumns());

    // Rows.
    $elements = $this->schema
      ->getElements($webform);
    foreach ($elements as $element) {
      $element['options_text'] = implode($multiple_delimiter, $element['options_text']);
      $element['options_value'] = implode($multiple_delimiter, $element['options_value']);
      fputcsv($handle, $element);
    }
    fclose($handle);
  });
  $response->headers
    ->set('Content-Type', 'application/force-download');
  $response->headers
    ->set('Content-Disposition', 'attachment; filename="' . $webform
    ->id() . '.schema.csv"');
  return $response;
}