forked from farmOS/farmOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
farm.install
148 lines (131 loc) · 4.39 KB
/
farm.install
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @file
* Install, update and uninstall functions for the farmOS installation profile.
*/
use Drupal\Component\Serialization\Json;
/**
* Implements hook_install_tasks().
*/
function farm_install_tasks(&$install_state) {
// Add tasks for configuring farmOS and enabling modules.
$tasks = [
'farm_install_config' => [
'type' => 'normal',
],
'\Drupal\farm\Form\FarmModulesForm' => [
'display_name' => t('Install modules'),
'type' => 'form',
],
'farm_install_modules' => [
'type' => 'batch',
],
];
return $tasks;
}
/**
* Implements hook_install_tasks_alter().
*/
function farm_install_tasks_alter(&$tasks, $install_state) {
// Override install task display names to replace "site" with "farmOS".
$alter_display_names = [
'install_profile_modules' => t('Install farmOS'),
'install_configure_form' => t('Configure farmOS'),
];
foreach ($alter_display_names as $task => $display_name) {
if (!empty($tasks[$task]['display_name'])) {
$tasks[$task]['display_name'] = $display_name;
}
}
}
/**
* Install default farmOS configuration.
*/
function farm_install_config() {
// Use private file system by default.
\Drupal::configFactory()->getEditable('system.file')->set('default_scheme', 'private')->save();
// Only allow admins to register new users.
\Drupal::configFactory()->getEditable('user.settings')->set('register', 'admin_only')->save();
}
/**
* Installs farmOS modules via a batch process.
*
* @param array $install_state
* An array of information about the current installation state.
*
* @return array
* The batch definition.
*/
function farm_install_modules(array &$install_state) {
// Load the list of farmOS core modules.
$all = farm_modules();
// Load the list of modules that should be installed.
// If provided, use the modules defined in farm.modules profile argument.
// We assume this is an array of module machine names, unless it is simply a
// string, which is interpreted as a shortcut for installing a set of modules.
// Available shortcuts are:
// - "all" (installs all modules)
// - "default" (installs default and base modules)
// - "base" (installs base modules only)
if (!empty($install_state['forms']['farm']['modules'])) {
$modules_arg = $install_state['forms']['farm']['modules'];
$all = farm_modules();
if ($modules_arg === 'all') {
$modules = array_merge(array_keys($all['base']), array_keys($all['default']), array_keys($all['optional']));
}
elseif ($modules_arg === 'default') {
$modules = array_merge(array_keys($all['base']), array_keys($all['default']));
}
elseif ($modules_arg === 'base') {
$modules = array_keys($all['base']);
}
else {
$modules = Json::decode($modules_arg);
if (!is_array($modules)) {
$modules = [];
}
}
}
// Use the state set by the \Drupal\farm\Form\FarmModulesForm submit method.
// Merge base modules into it.
else {
$modules = \Drupal::state()->get('farm.install_modules') ?: [];
$modules = array_merge(array_keys($all['base']), $modules);
}
// If this is running in the context of a functional test, do not install any
// additional modules. This is a temporary hack.
// @see Drupal\Tests\farm_test\Functional\FarmBrowserTestBase::setUp()
// @todo https://www.drupal.org/project/farm/issues/3183739
if (!empty($GLOBALS['farm_test'])) {
$modules = [];
}
// Load a list of all available modules, so that we can display their names.
$module_handler = \Drupal::service('module_handler');
// Assemble the batch operation for installing modules.
$operations = [];
foreach ($modules as $module) {
$operations[] = [
'_farm_install_module_batch',
[
$module,
$module_handler->getName($module),
],
];
}
$batch = [
'operations' => $operations,
'title' => t('Installing @drupal modules', ['@drupal' => drupal_install_profile_distribution_name()]),
'error_message' => t('The installation has encountered an error.'),
];
return $batch;
}
/**
* Implements callback_batch_operation().
*
* Performs batch installation of farmOS modules.
*/
function _farm_install_module_batch($module, $module_name, &$context) {
\Drupal::service('module_installer')->install([$module], TRUE);
$context['results'][] = $module;
$context['message'] = t('Installed %module module.', ['%module' => $module_name]);
}