WebformEntityExportForm.php in Webform 6.x
File
src/WebformEntityExportForm.php
View source
<?php
namespace Drupal\webform;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Utility\WebformYaml;
use Symfony\Component\HttpFoundation\Response;
class WebformEntityExportForm extends EntityForm {
public function form(array $form, FormStateInterface $form_state) {
$form['yaml'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => $this
->t("Here is your webform's configuration:"),
'#description' => $this
->t('Filename: %file', [
'%file' => $this
->getConfigName() . '.yml',
]),
'#default_value' => $this
->getYaml(),
];
return $form;
}
protected function actionsElement(array $form, FormStateInterface $form_state) {
$element['download'] = [
'#type' => 'submit',
'#value' => $this
->t('Download'),
'#button_type' => 'primary',
];
return $element;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$content = $this
->getYaml();
$filename = $this
->getConfigName() . '.yml';
$headers = [
'Content-Type' => 'text/yaml',
'Content-Disposition' => sprintf('attachment; filename="%s"', $filename),
];
$response = new Response($content, 200, $headers);
$form_state
->setResponse($response);
}
protected function getYaml() {
$config_name = $this
->getConfigName();
$data = $this
->config($config_name)
->getRawData();
return WebformYaml::encode($data);
}
protected function getConfigName() {
$webform = $this->entity;
$definition = $this->entityTypeManager
->getDefinition('webform');
return $definition
->getConfigPrefix() . '.' . $webform
->getConfigTarget();
}
}