forked from drupalwxt/wxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwxt.profile
143 lines (124 loc) · 4.34 KB
/
wxt.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
<?php
/**
* @file
* Contains wxt.profile.
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\wxt\Installer\Form\ExtensionConfigureForm;
use Symfony\Component\Yaml\Parser;
/**
* Implements hook_install_tasks().
*/
function wxt_install_tasks() {
return [
'wxt_install_extensions' => [
'display_name' => t('Install extensions'),
'display' => TRUE,
'type' => 'batch',
],
'wxt_import_language_config' => [
'display_name' => t('Import language configuration'),
'display' => TRUE,
],
];
}
/**
* Implements hook_install_tasks_alter().
*/
function wxt_install_tasks_alter(array &$tasks, array $install_state) {
$task = $tasks['wxt_import_language_config'];
unset($tasks['wxt_import_language_config']);
$tasks = array_merge($tasks, ['wxt_import_language_config' => $task]);
$task_keys = array_keys($tasks);
$insert_before = array_search('wxt_install_extensions', $task_keys, TRUE);
$tasks = array_slice($tasks, 0, $insert_before - 1, TRUE) +
[
'wxt_extension_configure_form' => [
'display_name' => t('Select extensions to enable'),
'type' => 'form',
'function' => ExtensionConfigureForm::class,
],
] +
array_slice($tasks, $insert_before - 1, NULL, TRUE);
}
/**
* Install task callback prepares a batch job to install WxT extensions.
*
* @param array $install_state
* The current install state.
*
* @return array
* The batch job definition.
*/
function wxt_install_extensions(array &$install_state) {
$batch = [];
$modules = \Drupal::state()->get('wxt_install_extensions', []);
$install_core_search = TRUE;
foreach ($modules as $module) {
$batch['operations'][] = ['wxt_install_module', (array) $module];
if ($module == 'wxt_ext_search_db') {
$install_core_search = FALSE;
}
}
if ($install_core_search) {
$batch['operations'][] = ['wxt_install_module', (array) 'search'];
// Enable default permissions for system roles.
user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, [
'use search',
'search content',
]);
}
return $batch;
}
/**
* Batch API callback. Installs a module.
*
* @param string|array $module
* The name(s) of the module(s) to install.
*/
function wxt_install_module($module) {
\Drupal::service('module_installer')->install((array) $module);
}
/**
* Install task callback; prepares a batch job to import language config.
*
* @param array $install_state
* The current install state.
*/
function wxt_import_language_config(array &$install_state) {
$language_manager = \Drupal::languageManager();
$yaml_parser = new Parser();
// The language code of the default locale.
$site_default_langcode = $language_manager->getDefaultLanguage()->getId();
$files = \Drupal::service('file_system')->scanDirectory(drupal_get_path('profile', 'wxt'), '/.*\.info\.yml/');
foreach ($files as $file) {
// The directory where the language config files reside.
$language_config_directory = dirname($file->uri) . '/config/install/language';
// Sub-directory names (language codes).
// The language code of the default language is excluded. If the user
// chooses to install in French etc, the language config is imported by core
// and the user has the chance to override it during the installation.
if (is_dir($language_config_directory)) {
$langcodes = array_diff(scandir($language_config_directory),
['..', '.', $site_default_langcode]);
foreach ($langcodes as $langcode) {
// All .yml files in the language's config subdirectory.
$config_files = glob("$language_config_directory/$langcode/*.yml");
foreach ($config_files as $file_name) {
// Information from the .yml file as an array.
$yaml = $yaml_parser->parse(file_get_contents($file_name));
// Uses the base name of the .yml file to get the config name.
$config_name = basename($file_name, '.yml');
/** @var \Drupal\language\ConfigurableLanguageManager $language_manager */
$config = $language_manager->getLanguageConfigOverride($langcode, $config_name);
foreach ($yaml as $config_key => $config_value) {
// Updates the configuration object.
$config->set($config_key, $config_value);
}
// Saves the configuration.
$config->save();
}
}
}
}
}