You are here

public function ConfigSingleExportForm::buildForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/config/src/Form/ConfigSingleExportForm.php \Drupal\config\Form\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 77

Class

ConfigSingleExportForm
Provides a form for exporting a single configuration file.

Namespace

Drupal\config\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $config_type = NULL, $config_name = NULL) {
  $form['#prefix'] = '<div id="js-config-form-wrapper">';
  $form['#suffix'] = '</div>';
  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,
    '#ajax' => [
      'callback' => '::updateConfigurationType',
      'wrapper' => 'js-config-form-wrapper',
    ],
  ];
  $default_type = $form_state
    ->getValue('config_type', $config_type);
  $form['config_name'] = [
    '#title' => $this
      ->t('Configuration name'),
    '#type' => 'select',
    '#options' => $this
      ->findConfiguration($default_type),
    '#default_value' => $config_name,
    '#prefix' => '<div id="edit-config-type-wrapper">',
    '#suffix' => '</div>',
    '#ajax' => [
      'callback' => '::updateExport',
      'wrapper' => 'edit-export-wrapper',
    ],
  ];
  $form['export'] = [
    '#title' => $this
      ->t('Here is your configuration:'),
    '#type' => 'textarea',
    '#rows' => 24,
    '#prefix' => '<div id="edit-export-wrapper">',
    '#suffix' => '</div>',
  ];
  if ($config_type && $config_name) {
    $fake_form_state = (new FormState())
      ->setValues([
      'config_type' => $config_type,
      'config_name' => $config_name,
    ]);
    $form['export'] = $this
      ->updateExport($form, $fake_form_state);
  }
  return $form;
}