SettingsForm.php in Module Builder 8.3
File
src/Form/SettingsForm.php
View source
<?php
namespace Drupal\module_builder\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class SettingsForm extends ConfigFormBase {
public function getFormId() {
return 'module_builder_settings';
}
protected function getEditableConfigNames() {
return [
'module_builder.settings',
];
}
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 ?? []);
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);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->config('module_builder.settings')
->set('data_directory', $form_state
->getValue('data_directory'))
->set('generator_settings.module', $form_state
->getValue('generator_settings_module'))
->save();
parent::submitForm($form, $form_state);
}
}