This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent_guide.profile
443 lines (406 loc) · 15.9 KB
/
student_guide.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
<?php
/**
* @file
* Enables modules and site configuration for a minimal site installation.
*/
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\Config\StorageComparer;
use Drupal\Core\Config\ConfigImporter;
use Drupal\Core\Config\ConfigImporterException;
use Drupal\Core\Site\Settings;
use Drupal\student_guide\Storage\SourceStorage;
use Drupal\block_content\Entity\BlockContent;
/**
* Need to do a manual include since this install profile never actually gets
* installed so therefore its code cannot be autoloaded.
*/
include_once __DIR__ . '/src/Form/SiteConfigureForm.php';
include_once __DIR__ . '/src/Form/SyncConfigureForm.php';
include_once __DIR__ . '/src/Storage/SourceStorage.php';
/**
* Implements hook_install_tasks_alter().
*/
function student_guide_install_tasks_alter(&$tasks, $install_state) {
$key = array_search('install_profile_modules', array_keys($tasks));
unset($tasks['install_profile_modules']);
unset($tasks['install_profile_themes']);
unset($tasks['install_install_profile']);
$config_tasks = [
'student_guide_upload' => [
'display_name' => t('Upload config'),
'type' => 'form',
'function' => 'Drupal\student_guide\Form\SyncConfigureForm',
],
'config_install_batch' => [
'display_name' => t('Install configuration'),
'type' => 'batch',
],
'student_guide_fix_profile' => [],
];
$tasks = array_slice($tasks, 0, $key, TRUE) +
$config_tasks +
array_slice($tasks, $key, NULL, TRUE);
$tasks['install_configure_form']['function'] = 'Drupal\student_guide\Form\SiteConfigureForm';
}
/**
* Creates a batch for the config importer to process.
*
* @see student_guide_install_tasks_alter()
*/
function config_install_batch() {
// We need to manually trigger the installation of core-provided entity types,
// as those will not be handled by the module installer.
// @see install_profile_modules()
install_core_entity_type_definitions();
// Create a source storage that reads from sync.
$listing = new ExtensionDiscovery(\Drupal::root());
$listing->setProfileDirectories([]);
$sync = new SourceStorage(\Drupal::service('config.storage.sync'), $listing->scan('profile'));
// Match up the site uuids, the install_base_system install task will have
// installed the system module and created a new UUID.
$system_site = $sync->read('system.site');
\Drupal::configFactory()->getEditable('system.site')->set('uuid', $system_site['uuid'])->save();
// Create the storage comparer and the config importer.
$config_manager = \Drupal::service('config.manager');
$storage_comparer = new StorageComparer($sync, \Drupal::service('config.storage'), $config_manager);
$storage_comparer->createChangelist();
$config_importer = new ConfigImporter(
$storage_comparer,
\Drupal::service('event_dispatcher'),
$config_manager,
\Drupal::service('lock.persistent'),
\Drupal::service('config.typed'),
\Drupal::service('module_handler'),
\Drupal::service('module_installer'),
\Drupal::service('theme_handler'),
\Drupal::service('string_translation')
);
try {
$sync_steps = $config_importer->initialize();
// Implementing hook_config_import_steps_alter() in this file does not work
// if using the 'drush site-install' command. Add the step to fix the import
// profile before the last step of the configuration import.
$last = array_pop($sync_steps);
$sync_steps[] = 'student_guide_install_uninstalled_profile_dependencies';
$sync_steps[] = 'student_guide_config_import_profile';
$sync_steps[] = $last;
$batch = [
'operations' => [],
'finished' => 'config_install_batch_finish',
'title' => t('Synchronizing configuration'),
'init_message' => t('Starting configuration synchronization.'),
'progress_message' => t('Completed @current step of @total.'),
'error_message' => t('Configuration synchronization has encountered an error.'),
'file' => drupal_get_path('module', 'config') . '/config.admin.inc',
];
foreach ($sync_steps as $sync_step) {
$batch['operations'][] = [
'config_install_batch_process',
[$config_importer, $sync_step],
];
}
return $batch;
}
catch (ConfigImporterException $e) {
// There are validation errors.
\Drupal::messenger()->addStatus(\Drupal::translation()->translate('The configuration synchronization failed validation.'));
foreach ($config_importer->getErrors() as $message) {
\Drupal::messenger()->addError($message);
}
}
}
/**
* Processes the config import batch and persists the importer.
*
* @param \Drupal\Core\Config\ConfigImporter $config_importer
* The batch config importer object to persist.
* @param string $sync_step
* The synchronisation step to do.
* @param $context
* The batch context.
*
* @see config_install_batch()
*/
function config_install_batch_process(ConfigImporter $config_importer, $sync_step, &$context) {
if (!isset($context['sandbox']['config_importer'])) {
$context['sandbox']['config_importer'] = $config_importer;
}
$config_importer = $context['sandbox']['config_importer'];
$config_importer->doSyncStep($sync_step, $context);
if ($errors = $config_importer->getErrors()) {
if (!isset($context['results']['errors'])) {
$context['results']['errors'] = [];
}
$context['results']['errors'] += $errors;
}
}
/**
* Finish config importer batch.
*
* @see config_install_batch()
*/
function config_install_batch_finish($success, $results, $operations) {
if ($success) {
if (!empty($results['errors'])) {
foreach ($results['errors'] as $error) {
\Drupal::messenger()->addError($error);
\Drupal::logger('config_sync')->error($error);
}
\Drupal::messenger()->addWarning(\Drupal::translation()->translate('The configuration was imported with errors.'));
}
else {
// Configuration sync needs a complete cache flush.
drupal_flush_all_caches();
}
}
else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
$message = \Drupal::translation()
->translate('An error occurred while processing %error_operation with arguments: @arguments', [
'%error_operation' => $error_operation[0],
'@arguments' => print_r($error_operation[1], TRUE),
]);
\Drupal::messenger()->addError($message);
}
}
/**
* Ensures all profile dependencies are created.
*
* @param array $context.
* The batch context.
* @param \Drupal\Core\Config\ConfigImporter $config_importer
* The config importer.
*
* @see config_install_batch()
*/
function student_guide_install_uninstalled_profile_dependencies(array &$context, ConfigImporter $config_importer) {
if (!array_key_exists('missing_profile_dependencies', $context)) {
$profile = _student_guide_get_original_install_profile();
$profile_file = drupal_get_path('profile', $profile) . "/$profile.info.yml";
$info = \Drupal::service('info_parser')->parse($profile_file);
$context['missing_profile_dependencies'] = array_diff($info['dependencies'], array_keys(\Drupal::moduleHandler()->getModuleList()));
if (count($context['missing_profile_dependencies']) === 0) {
$context['finished'] = 1;
return;
}
// @todo Need to dependency sort them...
}
$still_missing = array_diff($context['missing_profile_dependencies'], array_keys(\Drupal::moduleHandler()->getModuleList()));
if (!empty($still_missing)) {
$missing_module = array_shift($still_missing);
// This is not a config sync module install... fun!
\Drupal::service('config.installer')->setSyncing(FALSE);
\Drupal::service('module_installer')->install([$missing_module]);
$context['message'] = t('Installed @module. This will be uninstalled after the profile has been installed.', ['@module' => $missing_module]);
}
$context['finished'] = (count($context['missing_profile_dependencies']) - count($still_missing)) / count($context['missing_profile_dependencies']);
}
/**
* Processes profile as part of configuration sync.
*
* @param array $context.
* The batch context.
* @param \Drupal\Core\Config\ConfigImporter $config_importer
* The config importer.
*
* @see config_install_batch()
*/
function student_guide_config_import_profile(array &$context, ConfigImporter $config_importer) {
$orginal_profile = _student_guide_get_original_install_profile();
if ($orginal_profile) {
\Drupal::service('config.installer')
->setSyncing(TRUE)
->setSourceStorage($config_importer->getStorageComparer()->getSourceStorage());
\Drupal::service('module_installer')->install([$orginal_profile], FALSE);
module_set_weight($orginal_profile, 1000);
$context['message'] = t('Synchronising install profile: @name.', ['@name' => $orginal_profile]);
}
$context['finished'] = 1;
}
/**
* Fixes configuration if the install profile has made changes in hook_install().
*
* @see student_guide_install_tasks_alter()
*/
function student_guide_fix_profile() {
global $install_state;
// It is possible that installing the profile makes unintended configuration
// changes.
$config_manager = \Drupal::service('config.manager');
$storage_comparer = new StorageComparer(\Drupal::service('config.storage.sync'), \Drupal::service('config.storage'), $config_manager);
$storage_comparer->createChangelist();
if ($storage_comparer->hasChanges()) {
// Swap out the install profile so that the profile module exists.
_student_guide_switch_profile(_student_guide_get_original_install_profile());
\Drupal::service('extension.list.profile')->reset();
\Drupal::service('extension.list.module')->reset();
\Drupal::service('extension.list.theme_engine')->reset();
\Drupal::service('extension.list.theme')->reset();
$config_importer = new ConfigImporter(
$storage_comparer,
\Drupal::service('event_dispatcher'),
$config_manager,
\Drupal::service('lock.persistent'),
\Drupal::service('config.typed'),
\Drupal::service('module_handler'),
\Drupal::service('module_installer'),
\Drupal::service('theme_handler'),
\Drupal::service('string_translation')
);
try {
$config_importer->import();
}
catch (ConfigImporterException $e) {
// There are validation errors.
\Drupal::messenger()->addStatus(\Drupal::translation()->translate('The configuration synchronization failed validation.'));
foreach ($config_importer->getErrors() as $message) {
\Drupal::messenger()->addError($message);
}
}
// Replace the install profile so that the student_guide still works.
_student_guide_switch_profile('student_guide');
\Drupal::service('extension.list.profile')->reset();
\Drupal::service('extension.list.module')->reset();
\Drupal::service('extension.list.theme_engine')->reset();
\Drupal::service('extension.list.theme')->reset();
}
}
/**
* Switch the currently active profile in the installer.
*
* @param string $profile
* The profile to switch to.
*/
function _student_guide_switch_profile($profile) {
global $install_state;
$install_state['parameters']['profile'] = $profile;
$settings = Settings::getAll();
$settings['install_profile'] = $profile;
new Settings($settings);
}
/**
* Gets the original install profile name.
*
* @return string|null
* The name of the install profile from the sync configuration.
*/
function _student_guide_get_original_install_profile() {
$original_profile = NULL;
// Profiles need to be extracted from the install list if they are there.
// This is because profiles need to be installed after all the configuration
// has been processed.
$listing = new ExtensionDiscovery(\Drupal::root());
$listing->setProfileDirectories([]);
// Read directly from disk since the source storage in the config importer is
// being altered to exclude profiles.
$new_extensions = \Drupal::service('config.storage.sync')
->read('core.extension')['module'];
$profiles = array_intersect_key($listing->scan('profile'), $new_extensions);
if (!empty($profiles)) {
// There can be only one.
$original_profile = key($profiles);
}
return $original_profile;
}
/**
* Create content block for front page text (Instructions for students).
*/
function student_guide_create_front_page_text_block() {
$text_fi = 'Opas antaa sinulle kaiken opinnoissa tarvitsemasi tiedon. Voit hakea tietoa joko vapaan haun tai valmiiksi koostettujen teemojen avulla.';
$text_sv = 'Add swedish translation';
$text_en = 'Add english translation';
// get block uuid from config, see https://www.drupal.org/node/2756331
$plugin = \Drupal::config('block.block.frontpagetextblock')->get('plugin');
$uuid = substr($plugin, strpos($plugin, ':') + 1);
$block = BlockContent::create([
'info' => 'Front page text block',
'type' => 'content_block',
'langcode' => 'fi',
'uuid' => $uuid,
]);
$block->set('field_content_block_text', $text_fi);
$block->addTranslation('sv', ['field_content_block_text' => $text_sv]);
$block->addTranslation('en', ['field_content_block_text' => $text_en]);
$block->save();
}
/**
* Create content block for front page additional text (Instructions for
* students).
*/
function student_guide_create_front_page_additional_text_block() {
$block = BlockContent::create([
'info' => 'Front page additional text block',
'type' => 'content_block',
'langcode' => 'fi',
'uuid' => 'ecd71681-cd04-4ee0-b9b2-015cc2a19ff0',
]);
$block->set('field_content_block_text', '');
$block->addTranslation('sv', ['field_content_block_text' => '']);
$block->addTranslation('en', ['field_content_block_text' => '']);
$block->save();
}
/**
* Create content block for front page text (Instructions for teaching).
*/
function student_guide_create_teaching_front_page_text_block() {
$block = BlockContent::create([
'info' => 'Front page text block: Teaching',
'type' => 'content_block',
'langcode' => 'fi',
'uuid' => '46c7c03f-88cb-42bd-ab54-e8ac5cce8bab',
]);
$block->set('field_content_block_text', '');
$block->addTranslation('sv', ['field_content_block_text' => '']);
$block->addTranslation('en', ['field_content_block_text' => '']);
$block->save();
}
/**
* Create content block for front page additional text (Instructions for
* teaching).
*/
function student_guide_create_teaching_front_page_additional_text_block() {
$block = BlockContent::create([
'info' => 'Front page additional text block: Teaching',
'type' => 'content_block',
'langcode' => 'fi',
'uuid' => '56a05a0a-10f0-4c28-bd26-63e5e2e82768',
]);
$block->set('field_content_block_text', '');
$block->addTranslation('sv', ['field_content_block_text' => '']);
$block->addTranslation('en', ['field_content_block_text' => '']);
$block->save();
}
/**
* Create content block for Degree programme selector description text
* (Instructions for students).
*/
function student_guide_create_students_degree_programme_selection_description_text_block() {
$block = BlockContent::create([
'info' => 'Degree programme selector description text block: Students',
'type' => 'content_block',
'langcode' => 'fi',
'uuid' => 'd95bab6e-3e46-4c10-ba3e-fcb69254b11d',
]);
$block->set('field_content_block_text', '');
$block->addTranslation('sv', ['field_content_block_text' => '']);
$block->addTranslation('en', ['field_content_block_text' => '']);
$block->save();
}
/**
* Create content block for Degree programme selector description text
* (Instructions for teaching).
*/
function student_guide_create_teaching_degree_programme_selection_description_text_block() {
$block = BlockContent::create([
'info' => 'Degree programme selector description text block: Teaching',
'type' => 'content_block',
'langcode' => 'fi',
'uuid' => '572ed55b-455d-4d67-b726-7905452987e4',
]);
$block->set('field_content_block_text', '');
$block->addTranslation('sv', ['field_content_block_text' => '']);
$block->addTranslation('en', ['field_content_block_text' => '']);
$block->save();
}