You are here

public function SettingsForm::buildForm in Module Builder 8.3

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 ConfigFormBase::buildForm

File

src/Form/SettingsForm.php, line 32

Class

SettingsForm
Form for configuring Module Builder.

Namespace

Drupal\module_builder\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('module_builder.settings');
  $form['data_directory'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Module builder data directory'),
    '#field_prefix' => 'public://',
    '#default_value' => $config
      ->get('data_directory'),
    '#description' => $this
      ->t("The location to store Module Builder's processed data within the site's files directory."),
    '#required' => TRUE,
  );
  $form['generator_settings_module'] = [
    '#type' => 'details',
    '#title' => $this
      ->t("Module generation settings"),
    '#description' => $this
      ->t("These settings apply to all generated module code."),
    '#open' => TRUE,
    '#tree' => TRUE,
  ];
  $data = \Drupal::service('module_builder.drupal_code_builder')
    ->getTask('Configuration')
    ->getConfigurationData('module');
  $module_config = $config
    ->get('generator_settings.module');
  $data
    ->import($module_config ?? []);

  // We can be quite simplistic here, as (so far!) configuration from the
  // root component is just one complex data item with simple properties.
  foreach ($data as $key => $setting) {
    if ($setting
      ->getType() == 'boolean') {
      $element_type = 'checkbox';
    }
    else {
      $element_type = 'textfield';
    }
    $form['generator_settings_module'][$key] = [
      '#type' => $element_type,
      '#title' => $setting
        ->getLabel(),
      '#description' => $setting
        ->getDescription(),
      '#default_value' => $setting->value,
    ];
  }
  return parent::buildForm($form, $form_state);
}