function ConfigSingleExportForm::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 FormInterface::buildForm
File
-
core/
modules/ config/ src/ Form/ ConfigSingleExportForm.php, line 78
Class
- ConfigSingleExportForm
- Provides a form for exporting a single configuration file.
Namespace
Drupal\config\FormCode
public function buildForm(array $form, FormStateInterface $form_state, string $config_type = '', string $config_name = '') {
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type => $definition) {
if ($definition->entityClassImplements(ConfigEntityInterface::class)) {
$this->definitions[$entity_type] = $definition;
}
}
$entity_types = array_map(function (EntityTypeInterface $definition) {
return $definition->getLabel();
}, $this->definitions);
// Sort the entity types by label, then add the simple config to the top.
uasort($entity_types, 'strnatcasecmp');
$config_types = [
'system.simple' => $this->t('Simple configuration'),
] + $entity_types;
$form['config_type'] = [
'#title' => $this->t('Configuration type'),
'#type' => 'select',
'#options' => $config_types,
'#default_value' => $config_type,
];
// The config_name element depends on the value of config_type.
// Select and replace the wrapper element of the <select> tag
$form_url = Url::fromRoute('config.export_single', [
'config_type' => $config_type,
'config_name' => $config_name,
]);
(new Htmx())->post($form_url)
->select('*:has(>select[name="config_name"])')
->target('*:has(>select[name="config_name"])')
->swap('outerHTML')
->applyTo($form['config_type']);
$default_type = $form_state->getValue('config_type', $config_type);
$form['config_name'] = [
'#title' => $this->t('Configuration name'),
'#type' => 'select',
'#options' => $this->findConfiguration($default_type),
'#empty_option' => $this->t('- Select -'),
'#default_value' => $config_name,
];
// The export element depends on the value of config_type and config_name.
// Select and replace the wrapper element of the export textarea.
(new Htmx())->post($form_url)
->select('[data-export-wrapper]')
->target('[data-export-wrapper]')
->swap('outerHTML')
->applyTo($form['config_name']);
$form['export'] = [
'#title' => $this->t('Here is your configuration:'),
'#type' => 'textarea',
'#rows' => 24,
'#wrapper_attributes' => [
'data-export-wrapper' => TRUE,
],
];
$pushUrl = FALSE;
$trigger = $this->getHtmxTriggerName();
if ($trigger == 'config_type') {
$form = $this->updateConfigurationType($form, $form_state);
// Also update the empty export element "out of band".
(new Htmx())->swapOob('outerHTML:[data-export-wrapper]')
->applyTo($form['export'], '#wrapper_attributes');
$pushUrl = Url::fromRoute('config.export_single', [
'config_type' => $default_type,
'config_name' => '',
]);
}
elseif ($trigger == 'config_name') {
// A name is selected.
$default_name = $form_state->getValue('config_name', $config_name);
$form['export'] = $this->updateExport($form, $default_type, $default_name);
// Update the url in the browser location bar.
$pushUrl = Url::fromRoute('config.export_single', [
'config_type' => $default_type,
'config_name' => $default_name,
]);
}
elseif ($config_type && $config_name) {
// We started with values, update the export using those.
$form['export'] = $this->updateExport($form, $config_type, $config_name);
}
if ($pushUrl) {
(new Htmx())->pushUrlHeader($pushUrl)
->applyTo($form);
}
return $form;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.