Add moduleOptions helper function to make FarmModulesForm reusable.

This commit is contained in:
paul121 2020-09-10 10:35:24 -07:00
parent 8d0888802f
commit 986894a82e
1 changed files with 36 additions and 10 deletions

View File

@ -55,14 +55,8 @@ class FarmModulesForm extends FormBase {
// Set the form title.
$form['#title'] = $this->t('Enable modules');
// Load the list of available modules.
$modules = farm_modules();
// Allow user to choose which high-level farm modules to install.
$module_options = array_merge($modules['default'], $modules['optional']);
// Default modules will be selected by default.
$module_defaults = array_keys($modules['default']);
// Load module options.
$modules = $this->moduleOptions();
// Module checkboxes.
$form['modules'] = [
@ -70,10 +64,15 @@ class FarmModulesForm extends FormBase {
'#title_display' => 'invisible',
'#type' => 'checkboxes',
'#description' => $this->t('Select the farmOS modules that you would like installed by default.'),
'#options' => $module_options,
'#default_value' => $module_defaults,
'#options' => $modules['options'],
'#default_value' => $modules['default'],
];
// Disable checkboxes for modules marked as disabled.
foreach ($modules['disabled'] as $name) {
$form['modules'][$name]['#disabled'] = TRUE;
}
// Submit button.
$form['actions'] = ['#type' => 'actions'];
$form['actions']['submit'] = [
@ -85,6 +84,33 @@ class FarmModulesForm extends FormBase {
return $form;
}
/**
* Helper function for building a list of modules to install.
*
* @return array
* Returns an array with three sub-arrays: 'options', 'default' and
* 'disabled'. All modules should be included in the 'options' array.
* Default modules will be selected for installation by default, and
* disabled modules cannot have their checkbox changed by users.
*/
protected function moduleOptions() {
// Load the list of available modules.
$modules = farm_modules();
// Allow user to choose which high-level farm modules to install.
$module_options = array_merge($modules['default'], $modules['optional']);
// Default modules will be selected by default.
$module_defaults = array_keys($modules['default']);
return [
'options' => $module_options,
'default' => $module_defaults,
'disabled' => [],
];
}
/**
* {@inheritdoc}
*/