public function WebformDevelEntityFormApiExportForm::buildForm in Webform 6.x
Same name and namespace in other branches
- 8.5 modules/webform_devel/src/Form/WebformDevelEntityFormApiExportForm.php \Drupal\webform_devel\Form\WebformDevelEntityFormApiExportForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides EntityForm::buildForm
File
- modules/
webform_devel/ src/ Form/ WebformDevelEntityFormApiExportForm.php, line 22
Class
- WebformDevelEntityFormApiExportForm
- Export a webform's element to Form API (FAPI).
Namespace
Drupal\webform_devel\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $this
->getEntity();
$elements = $webform
->getElementsDecoded();
$this
->cleanupElements($elements);
$this
->setDefaultValues($elements);
$elements['actions'] = [
'#type' => 'actions',
'#tree' => TRUE,
'submit' => [
'#type' => 'submit',
'#value' => (string) $this
->t('Save configuration'),
'#button_type' => 'primary',
],
];
$webform_id = $webform
->id();
$webform_label = $webform
->label();
$webform_form_name = str_replace('_', '', ucwords($webform_id, '_')) . 'SettingsForm';
// Filenames.
$file_names = [
'info' => "/{$webform_id}/{$webform_id}.info.yml",
'routing' => "/{$webform_id}/{$webform_id}.routing.yml",
'form' => "/{$webform_id}/src/Form/{$webform_form_name}.php",
'config' => "/{$webform_id}/config/install/{$webform_id}.settings.yml",
];
// Form.
$webform_elements = str_replace("\n", "\n ", trim($this
->renderExport($elements)));
$webform_elements = preg_replace("/'##([^#]+)##'/ims", '$config->get(\'$1\')', $webform_elements);
$build = [
'#type' => 'inline_template',
'#template' => $this
->getPhpTemplate(),
'#context' => [
'name' => $webform
->id(),
'label' => $webform
->label(),
'class_name' => str_replace('_', '', ucwords($webform
->id(), '_')),
'form' => Markup::create($webform_elements),
],
];
$form_php = $this->renderer
->render($build);
// Config.
$config = $this->generate
->getData($webform);
// Form.
$form['code'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Form API (FAPI) Code'),
'#file_names' => $file_names,
'#tree' => TRUE,
];
$form['code']['description'] = [
'#markup' => $this
->t('Learn more about <a href="https://www.drupal.org/docs/8/api/form-api">Form API in Drupal 8</a>.'),
'#prefix' => '<p>',
'#suffix' => '</p>',
];
// Info.
$form['code']['info'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => $this
->t('Module info'),
'#description' => $this
->t('Filename: %file', [
'%file' => $file_names['info'],
]),
'#help' => FALSE,
'#attributes' => [
'readonly' => TRUE,
],
'#default_value' => Yaml::encode([
'name' => $webform_label,
'type' => 'module',
'description' => $webform
->getDescription(),
'package' => 'Webform Custom',
'core' => '8.x',
'configure' => "{$webform_id}.settings",
'dependencies' => [
'webform:webform',
],
]),
];
// Routing.
$form['code']['routing'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => $this
->t('Routing'),
'#description' => $this
->t('Filename: %file', [
'%file' => $file_names['routing'],
]),
'#help' => FALSE,
'#attributes' => [
'readonly' => TRUE,
],
'#default_value' => Yaml::encode([
$webform_id . '.settings' => [
'path' => '/admin/config/' . $webform_id,
'defaults' => [
'_form' => "\\Drupal\\{$webform_id}\\Form\\{$webform_form_name}",
'_title' => $webform_label,
],
'requirements' => [
'_permission' => 'administer configuration',
],
],
]),
];
// Form (API).
$form['code']['form'] = [
'#type' => 'webform_codemirror',
'#mode' => 'php',
'#title' => $this
->t('Configuration settings form'),
'#description' => $this
->t('Filename: %file', [
'%file' => $file_names['form'],
]),
'#help' => FALSE,
'#attributes' => [
'readonly' => TRUE,
],
'#default_value' => $form_php,
];
// Config.
$form['code']['config'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => $this
->t('Default configuration'),
'#description' => $this
->t('Filename: %file', [
'%file' => $file_names['config'],
]),
'#help' => FALSE,
'#attributes' => [
'readonly' => TRUE,
],
'#default_value' => Yaml::encode($config),
];
$form['actions'] = [
'#type' => 'actions',
'download' => [
'#type' => 'submit',
'#value' => $this
->t('Download'),
'#button_type' => 'primary',
],
'test' => [
'#type' => 'link',
'#title' => $this
->t('Test'),
'#url' => Url::fromRoute('entity.webform.fapi_test_form', [
'webform' => $webform_id,
]),
'#attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_WIDE, [
'button',
]),
],
];
WebformDialogHelper::attachLibraries($form);
return $form;
}