-
Notifications
You must be signed in to change notification settings - Fork 20
/
droopler.install
347 lines (312 loc) · 9.18 KB
/
droopler.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/**
* @file
* Install, update and uninstall functions for the profilename install profile.
*/
declare(strict_types = 1);
use Drupal\Core\Config\FileStorage;
/**
* Implements hook_install().
*
* Perform actions to set up the site for this profile.
*
* @see system_install()
*/
function droopler_install() {
// First, do everything in standard profile.
include_once DRUPAL_ROOT . '/core/profiles/standard/standard.install';
standard_install();
\Drupal::configFactory()
->getEditable('node.settings')
->set('use_admin_theme', TRUE)
->save();
\Drupal::configFactory()
->getEditable('system.site')
->set('default_langcode', 'en')
->set('page.front', '/node/1')
->save();
/** @var \Drupal\Core\Extension\ModuleInstaller $module_installer */
$module_installer = \Drupal::service('module_installer');
/** @var \Drupal\Core\Extension\ModuleHandler $module_handler */
$module_handler = \Drupal::service('module_handler');
// Install init content. It will add an install task to be performed later.
$module_installer->install(['d_content_init']);
// Try to install custom init that alters the original one.
if ($module_handler->moduleExists('d_custom_init')) {
$module_installer->install(['d_custom_init']);
}
else {
\Drupal::logger('droopler_install')
->notice('Custom content init not detected.');
}
// Remove footer menu to avoid fatal error while trying to edit it
// because of missing route.
\Drupal::configFactory()->getEditable('system.menu.footer')->delete();
// Set droopler_theme as default.
\Drupal::configFactory()
->getEditable('system.theme')
->set('default', 'droopler_theme')
->set('admin', 'claro')
->save();
}
/**
* Implements hook_install_tasks().
*/
function droopler_install_tasks() {
$tasks = [
'droopler_additional_components_form' => [
'display_name' => t('Enable additional components'),
'type' => 'form',
'function' => 'Drupal\droopler\Installer\Form\AdditionalComponentsForm',
],
'droopler_additional_components_install' => [
'display_name' => t('Install additional components'),
'type' => 'batch',
],
'droopler_enable_subtheme_batch' => [
'display' => TRUE,
'display_name' => t('Install and enable subtheme'),
'type' => 'batch',
],
];
return $tasks;
}
/**
* Install additional components in a batch.
*
* @param array $install_state
* The install state.
*
* @return array
* Batch array.
*/
function droopler_additional_components_install(array &$install_state) {
$modules = $install_state['droopler_additional_modules'];
$init_content = $install_state['droopler_init_content'];
$batch = $operations = [];
if ($modules) {
foreach ($modules as $module) {
$operations[] = [
'droopler_install_module',
[$module],
];
}
}
if ($init_content) {
$init_content_batch = droopler_enable_init_content_batch();
foreach ($init_content_batch['operations'] as $operation) {
$operations[] = $operation;
}
}
if (!empty($operations)) {
$batch = [
'operations' => $operations,
'title' => t('Installing selected components'),
'error_message' => t('Installation error.'),
];
}
return $batch;
}
/**
* Allow to install a single module using module_installer service.
*
* @param string $module
* Module machine name.
* @param array $context
* Batch context.
*/
function droopler_install_module(string $module, array &$context) {
\Drupal::service('module_installer')->install([$module], TRUE);
$context['message'] = t('Installed %module module.', ['%module' => $module]);
}
/**
* Import a config file.
*
* @param string $module
* Module name.
* @param string $name
* Config file name without .yml extension.
*
* @todo To be moved to a new manager class.
*/
function _droopler_import_config($module, $name) {
$config_path = \Drupal::service('extension.path.resolver')->getPath('module', $module) . '/config/install';
$source = new FileStorage($config_path);
$config_storage = \Drupal::service('config.storage');
$config_storage->write($name, $source->read($name));
}
/**
* Install d_product.
*
* @param int $step
* Current installation step.
* @param array $context
* Batch context.
*/
function droopler_enable_product(int $step, array &$context) {
switch ($step) {
case 1:
droopler_install_module('d_product', $context);
break;
case 2:
\Drupal::service('config.installer')->installOptionalConfig();
$context['message'] = t('Optional config installed');
break;
}
}
/**
* Batch content init creating.
*
* @return array
* Batch defintion.
*/
function droopler_enable_init_content_batch() {
$types = [
'block',
'media',
'content',
'block_second_run',
'index_content',
];
$operations = [];
foreach ($types as $type) {
$operations[] = ['d_content_init_create_all_batch', [$type]];
}
return [
'operations' => $operations,
'title' => t('Init content'),
];
}
/**
* Batch enable of subtheme.
*
* @return array
* Batch defintion.
*/
function droopler_enable_subtheme_batch() {
return [
'operations' => [
['droopler_enable_subtheme', []],
['droopler_enable_subtheme_postprocess', []],
],
'title' => t('Install and enable subtheme'),
];
}
/**
* Enable subtheme.
*
* @param array $context
* Batch context.
*/
function droopler_enable_subtheme(array &$context) {
try {
\Drupal::service('theme_installer')->install(['droopler_subtheme']);
\Drupal::configFactory()
->getEditable('system.theme')
->set('default', 'droopler_subtheme')
->set('admin', 'claro')
->save();
$context['message'] = t('Installed %theme theme', ['%theme' => 'droopler_subtheme']);
}
catch (Exception $e) {
\Drupal::logger('droopler_install')->notice('No subtheme detected.');
$context['message'] = t('%theme theme not detected, skipping theme installation.', ['%theme' => 'droopler_subtheme']);
}
}
/**
* Additional operations after subtheme installation.
*
* All those operations have to be executed after installation of the subtheme.
* We can think about to place them in the droopler_subtheme.install file when
* this functionality will be implemented.
*
* @param array $context
* Batch context.
*
* @see: https://www.drupal.org/project/drupal/issues/2652542
* @see: https://www.drupal.org/node/2937955
*/
function droopler_enable_subtheme_postprocess(array &$context) {
if (!\Drupal::service('theme_handler')->themeExists('droopler_subtheme')) {
$context['message'] = t('Theme %theme not installed, skipping postinstall operations', ['%theme' => 'droopler_subtheme']);
}
else {
// Set logo same as on base theme if custom path has been defined there.
$base_settings = Drupal::configFactory()
->getEditable('droopler_theme.settings');
if (!$base_settings->get('logo.use_default')
&& $logo_path = $base_settings->get('logo.path')) {
$current_settings = Drupal::configFactory()
->getEditable('droopler_subtheme.settings');
$current_settings->set('logo.use_default', FALSE);
$current_settings->set('logo.path', $logo_path);
$current_settings->save();
}
$context['message'] = t('Theme %theme configured.', ['%theme' => 'droopler_subtheme']);
}
}
/**
* Performs config update of specified modules.
*
* Configs should be placed in respective module directories e.g
* "module/config/update/moduleName.updateNumber.yml".
*
* @param array $modules
* Array of module names to update.
* @param string|int $update_number
* Number of the update corresponding to the droopler update number.
*
* @return bool
* Status of the modules configuration update.
*/
function update_modules_configuration(array $modules, $update_number) {
$status = [];
$updater = \Drupal::service('d_update');
$module_handler = \Drupal::service('module_handler');
foreach ($modules as $module) {
if ($module_handler->moduleExists($module)) {
$update = $updater->updateConfigurations("module/{$module}", "{$module}.{$update_number}");
if (!$update) {
\Drupal::logger($module)->error('The configuration update for the module failed');
$status[] = $update;
}
}
}
return !in_array(FALSE, $status);
}
/**
* Returns array of names of all existing entities bundles.
*
* @return array
* Array with all entities bundles.
*/
function get_all_entity_bundles() {
$entityBundles = \Drupal::service('entity_type.bundle.info')
->getAllBundleInfo();
$bundles = [];
foreach ($entityBundles as $entityBundle) {
foreach ($entityBundle as $bundle => $information) {
$bundles[] = $bundle;
}
}
return $bundles;
}
/**
* Returns a list of modules to install, based on the list of required modules.
*
* @param array $modules
* Array of required modules.
*
* @return array
* Array of modules that actually need installing.
*/
function calculate_modules_for_install(array $modules) {
$module_handler = \Drupal::service('module_handler');
$modulesToInstall = [];
foreach ($modules as $module) {
if (!$module_handler->moduleExists($module)) {
$modulesToInstall[] = $module;
}
}
return $modulesToInstall;
}