forked from farmOS/farmOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFarmModulesForm.php
116 lines (96 loc) · 2.81 KB
/
FarmModulesForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Drupal\farm\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form for selecting farmOS modules to install.
*
* @ingroup farm
*/
class FarmModulesForm extends FormBase {
/**
* The state keyvalue collection.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Constructs a new FarmModulesForm.
*
* @param \Drupal\Core\State\StateInterface $state
* The state keyvalue collection to use.
*/
public function __construct(StateInterface $state) {
$this->state = $state;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('state'),
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'farm_modules_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Set the form title.
$form['#title'] = $this->t('Enable modules');
// Load module options.
$modules = $this->moduleOptions();
// Module checkboxes.
$form['modules'] = [
'#title' => $this->t('farmOS Modules'),
'#title_display' => 'invisible',
'#type' => 'checkboxes',
'#description' => $this->t('Select the farmOS modules that you would like to be installed.'),
'#options' => $modules['options'],
'#default_value' => $modules['default'],
];
// Submit button.
$form['actions'] = ['#type' => 'actions'];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save and continue'),
'#button_type' => 'primary',
];
return $form;
}
/**
* Helper function for building a list of modules to install.
*
* @return array
* Returns an array with two sub-arrays: 'options' and 'default'.
* All modules should be included in the 'options' array. Default modules
* will be selected for installation by default.
*/
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,
];
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Build array of module names to install.
$modules = array_values(array_filter($form_state->getValue('modules')));
$this->state->set('farm.install_modules', $modules);
}
}