-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdropsolid_rocketship_profile.profile
471 lines (412 loc) · 14.4 KB
/
dropsolid_rocketship_profile.profile
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?php
/**
* @file
* Profile main file.
*/
use Drupal\Component\Render\FormattableMarkup;
use Drupal\rocketship_core\Form\DefaultContentDefaultLanguage;
use Drupal\views\Views;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\webform\Entity\Webform;
use Drupal\block\Entity\Block;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\dropsolid_rocketship_profile\Form\AssemblerForm;
use Drupal\dropsolid_rocketship_profile\Form\ConfigureMultilingualForm;
use Drupal\dropsolid_rocketship_profile\Form\RocketshipSiteConfigureForm;
/**
* Implements hook_install_tasks_alter().
*/
function dropsolid_rocketship_profile_install_tasks_alter(&$tasks, $install_state) {
// Use our own configure form.
$tasks['install_configure_form']['function'] = RocketshipSiteConfigureForm::class;
// Add our own wrapper to install_profile_modules to suppress messages.
$tasks['install_profile_modules']['function'] = 'dropsolid_rocketship_profile_install_profile_modules';
$tasks['install_finished']['function'] = 'dropsolid_after_install_finished';
}
/**
* Implements hook_install_tasks().
*/
function dropsolid_rocketship_profile_install_tasks(&$install_state) {
// Determine whether the enable multilingual option is selected during the
// Multilingual configuration task.
$needs_configure_multilingual = (isset($install_state['dropsolid_rocketship_profile']['enable_multilingual']) && $install_state['dropsolid_rocketship_profile']['enable_multilingual'] == TRUE);
return [
'dropsolid_rocketship_profile_multilingual_configuration_form' => [
'display_name' => t('Multilingual configuration'),
'display' => TRUE,
'type' => 'form',
'function' => ConfigureMultilingualForm::class,
],
'dropsolid_rocketship_profile_configure_multilingual' => [
'display_name' => t('Configure multilingual'),
'display' => $needs_configure_multilingual,
'type' => 'batch',
],
'dropsolid_rocketship_profile_configure_default_content_default_language' => [
'display_name' => t('Configure Default Content Default Language'),
'type' => 'form',
'function' => DefaultContentDefaultLanguage::class,
],
'dropsolid_rocketship_profile_extra_components' => [
'display_name' => t('Extra components'),
'display' => TRUE,
'type' => 'form',
'function' => AssemblerForm::class,
],
'dropsolid_rocketship_profile_assemble_extra_components' => [
'display_name' => t('Assemble extra components'),
'display' => TRUE,
'type' => 'batch',
],
];
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function dropsolid_rocketship_profile_form_install_select_language_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$form['langcode']['#default_value'] = 'en';
$form['langcode']['#value'] = 'en';
$form['langcode']['#disabled'] = TRUE;
$form['info'] = [
'#type' => 'item',
// #markup is XSS admin filtered which ensures unsafe protocols will be
// removed from the url.
'#markup' => '<p>Rocketship is designed for a default language of English. Using the included <a target="_blank" href="https://www.drupal.org/project/disable_language">Disable Language</a> module you can restrict access to English later on, if needed.</p>',
];
}
/**
* Install modules.
*
* Wrapper around install_profile_modules to add a hide warning and status
* message operation at the end. We don't need to see any module info
* messages during site installation.
*
* @param array $install_state
* The current install state.
*
* @return array
* Return batch array.
*/
function dropsolid_rocketship_profile_install_profile_modules(array &$install_state) {
$batch = install_profile_modules($install_state);
// Add hide message as last batch.
$batch['operations'][] = [
'dropsolid_rocketship_profile_postpone_messages',
(array) TRUE,
];
return $batch;
}
/**
* Batch job to configure multilingual components.
*
* @param array $install_state
* The current install state.
*
* @return array
* The batch job definition.
*/
function dropsolid_rocketship_profile_configure_multilingual(array &$install_state) {
$batch = [];
// If the multilingual config checkbox was checked.
if (!empty($install_state['dropsolid_rocketship_profile']['enable_multilingual'])) {
// Add all selected languages.
foreach ($install_state['dropsolid_rocketship_profile']['multilingual_languages'] as $language_code) {
$batch['operations'][] = [
'dropsolid_rocketship_profile_enable_language',
(array) $language_code,
];
}
// Hide warnings and status messages.
$batch['operations'][] = [
'dropsolid_rocketship_profile_postpone_messages',
(array) TRUE,
];
}
// Fix entity updates to clear up any mismatched entity.
$batch['operations'][] = [
'dropsolid_rocketship_profile_fix_entity_update',
(array) TRUE,
];
return $batch;
}
/**
* Batch function to hide warning messages.
*/
function dropsolid_rocketship_profile_postpone_messages() {
global $_SESSION;
$messenger = \Drupal::messenger();
$messages = $messenger->all();
$messenger->deleteAll();
if (!isset($_SESSION['install_state']['dropsolid_rocketship_profile']['saved_messages'])) {
$_SESSION['install_state']['dropsolid_rocketship_profile']['saved_messages'] = [];
}
// Save all messages to output them at the end.
foreach ($messages as $type => $list) {
foreach ($list as $idx => $message) {
$needles = [
'This site has only a single language enabled',
'Enable translation for content types',
];
foreach ($needles as $needle) {
$message = strip_tags((string) $message);
if (strpos($message, $needle) === 0) {
unset($list[$idx]);
}
}
}
if (!isset($_SESSION['install_state']['dropsolid_rocketship_profile']['saved_messages'][$type])) {
$_SESSION['install_state']['dropsolid_rocketship_profile']['saved_messages'][$type] = [];
}
$_SESSION['install_state']['dropsolid_rocketship_profile']['saved_messages'][$type] = array_merge($_SESSION['install_state']['dropsolid_rocketship_profile']['saved_messages'][$type], $list);
}
}
/**
* Batch function to assemble and install needed extra components.
*
* @param string|array $extra_component
* Name of the extra component.
*/
function dropsolid_rocketship_profile_assemble_extra_component_then_install($extra_component) {
\Drupal::service('module_installer')->install((array) $extra_component, TRUE);
}
/**
* Batch function to add selected languages then fetch all translations.
*
* @param string|array $language_code
* Language code to install and fetch all traslation.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function dropsolid_rocketship_profile_enable_language($language_code) {
ConfigurableLanguage::createFromLangcode($language_code)->save();
}
/**
* Batch function to fix entity updates to clear up any mismatched entity.
*
* Entity and/or field definitions, The following changes were detected in
* the entity type and field definitions.
*
* @param string|array $entity_update
* To entity update or not.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function dropsolid_rocketship_profile_fix_entity_update($entity_update) {
// Removed call to deprecated function. Should no longer be needed either.
}
/**
* Batch job to assemble dropsolid_rocketship_profile extra components.
*
* @param array $install_state
* The current install state.
*
* @return array
* The batch job definition.
*/
function dropsolid_rocketship_profile_assemble_extra_components(array &$install_state) {
$modules = $install_state['dropsolid_rocketship_profile']['extra_features'] ?: [];
// Also chuck in any modules required by the selected theme.
/** @var \Drupal\Core\Extension\ThemeHandlerInterface $themeHandler */
$themeHandler = \Drupal::service('theme_handler');
$themes = $themeHandler->rebuildThemeData();
$theme = $themes[$GLOBALS['install_state']['dropsolid_rocketship_profile']['theme']];
$requirements = !empty($theme->info['requirements']) ? $theme->info['requirements'] : [];
$modules = array_merge($modules, $requirements);
$batch = _dropsolid_rocketship_profile_assemble_extra_components_batch($modules);
// Enable the theme as last.
$batch['operations'][] = [
'dropsolid_rocketship_profile_install_theme',
[$GLOBALS['install_state']['dropsolid_rocketship_profile']['theme']],
];
// Hide warnings and status messages.
$batch['operations'][] = [
'dropsolid_rocketship_profile_postpone_messages',
(array) TRUE,
];
return $batch;
}
/**
* Installs the chosen theme.
*
* @param string $theme
* Theme name.
*
*/
function dropsolid_rocketship_profile_install_theme($theme) {
\Drupal::service('theme_installer')->install([$theme]);
// Also set it as the default theme.
\Drupal::configFactory()->getEditable('system.theme')
->set('default', $theme)
->save();
}
/**
* Create batch array for list of modules to be installed.
*
* @param array $modules
* List of modules.
*
* @return array
* Batch array.
*/
function _dropsolid_rocketship_profile_assemble_extra_components_batch(array $modules) {
$files = \Drupal::service('extension.list.module')->reset()->getList();
// Always install required modules first. Respect the dependencies between
// the modules.
$required = [];
$non_required = [];
// Add modules that other modules depend on.
foreach ($modules as $module) {
if ($files[$module]->requires) {
$modules = array_merge($modules, array_keys($files[$module]->requires));
}
}
$modules = array_unique($modules);
foreach ($modules as $module) {
if (!empty($files[$module]->info['required'])) {
$required[$module] = $files[$module]->sort;
}
else {
$non_required[$module] = $files[$module]->sort;
}
}
arsort($required);
arsort($non_required);
$operations = [];
foreach ($required + $non_required as $module => $weight) {
$operations[] = [
'_install_module_batch',
[$module, $files[$module]->info['name']],
];
}
$batch = [
'operations' => $operations,
'title' => t('Installing @drupal', ['@drupal' => drupal_install_profile_distribution_name()]),
'error_message' => t('The installation has encountered an error.'),
];
// Hide warnings and status messages.
$batch['operations'][] = [
'dropsolid_rocketship_profile_postpone_messages',
(array) TRUE,
];
return $batch;
}
/**
* Runs after install is finished.
*
* @param array $install_state
* The current install state.
*
* @return array
* Renderable array to output.
*/
function dropsolid_after_install_finished(array &$install_state) {
global $_SESSION;
install_finished($install_state);
// Rebuild permissions.
node_access_rebuild();
// Delete "Contact" webform.
try {
$form = Webform::load('contact');
if ($form) {
$form->delete();
}
}
catch (EntityStorageException $e) {
// No biggie.
}
try {
// Delete frontpage view.
$view = Views::getView('frontpage');
$view->destroy();
$view->storage->delete();
}
catch (\Exception $e) {
// No biggie. Still part of checklist.
}
// Delete the congratulations message
// and node rebuild message.
$messenger = \Drupal::messenger();
$messenger->deleteAll();
$output = [
'#title' => t('Installation finished'),
'info' => [
'#type' => 'container',
'congratulations' => [
'#markup' => new FormattableMarkup('<p>' .
t('Congratulations, you have successfully installed Dropsolid Rocketship Profile') .
'</p>', []),
],
'drush_info' => [
'#markup' => new FormattableMarkup('<p><strong style="background-color: #ffa500">' .
t('If you wish to fully setup your local environment, please run the following drush command from just inside the docroot. It will setup all the required config split folders and populate them.') .
'</strong></p>', []),
],
'drush' => [
'#markup' => new FormattableMarkup('<pre><code style="background-color: #444040; color: #ffffff;display:block;padding:20px;">drush d-set</code></pre>', []),
],
],
'messages' => [
'#type' => 'details',
'#title' => t('Messages'),
'#description' => t('Messages output during install'),
],
'visit_site' => [
'#markup' => '<a href="/">' . t('Visit your website') . '</a>',
],
];
foreach ($_SESSION['install_state']['dropsolid_rocketship_profile']['saved_messages'] as $type => $messages) {
$output['messages'][$type] = [
'#theme' => 'item_list',
'#title' => t('Type: @type', ['@type' => $type]),
'#list_type' => 'ul',
'#items' => [],
'#attributes' => [
'class' => ['color-' . $type],
'style' => 'margin-bottom:15px;',
],
];
foreach ($messages as $message) {
// For some reason <front> turns into /core/install.php during
// installation so replace that part and reinsert into
// FormattableMarkup else it'll escape any HTML.
$message = str_replace('/core/install.php', '', (string) $message);
$output['messages'][$type]['#items'][] =
new FormattableMarkup($message, []);
if (_dropsolid_rocketship_profile_is_drupal_cli()) {
// Re-add the messages for CLI.
$messenger->addMessage($message, $type);
}
}
}
unset($_SESSION['install_state']['dropsolid_rocketship_profile']);
if (_dropsolid_rocketship_profile_is_drupal_cli()) {
$messenger->addMessage(t('If you wish to fully setup your local environment, please run the drush command "d-set" which will create and populate all the config split folders'), 'warning');
}
// Set Chosen include setting to only be included on admin pages
// It's set to everywhere in config so it'll work during the install.
\Drupal::configFactory()
->getEditable('chosen.settings')
->set('chosen_include', CHOSEN_INCLUDE_ADMIN)
->save();
// Clear all caches. Mostly because deleting frontpage /node
// view breaks that path until caches are cleared.
drupal_flush_all_caches();
return $output;
}
/**
* Check if Drupal is running in CLI.
*
* @see https://www.drupal.org/project/drupal/issues/2904700
*
* @return bool
* If Drupal is running in CLI.
*/
function _dropsolid_rocketship_profile_is_drupal_cli() {
if (defined('STDIN')) {
return TRUE;
}
if (in_array(PHP_SAPI, ['cli', 'cli-server', 'phpdbg'])) {
return TRUE;
}
return FALSE;
}