This repository has been archived by the owner on Mar 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pco_cities.profile
113 lines (99 loc) · 3.35 KB
/
pco_cities.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
<?php
/**
* @file
* Contains pco_cities.profile.
*/
use Symfony\Component\Yaml\Parser;
/**
* Implements hook_install_tasks().
*/
function pco_cities_install_tasks() {
return [
'pco_cities_install_extensions' => [
'display_name' => t('Install extensions'),
'display' => TRUE,
'type' => 'batch',
],
'pco_cities_import_language_config' => [
'display_name' => t('Import language configuration'),
'display' => TRUE,
],
];
}
/**
* Implements hook_install_tasks_alter().
*/
function pco_cities_install_tasks_alter(array &$tasks, array $install_state) {
// Moves the language config import task to the end of the install tasks so
// that it is run after the final import of languages.
$task = $tasks['pco_cities_import_language_config'];
unset($tasks['pco_cities_import_language_config']);
$tasks = array_merge($tasks, ['pco_cities_import_language_config' => $task]);
}
/**
* Install task callback; prepares a batch job to install CDS extensions.
*
* @param array $install_state
* The current install state.
*
* @return array
* The batch job definition.
*/
function pco_cities_install_extensions(array &$install_state) {
$batch = [];
$modules = [
'wxt_ext',
'pco_cities_ext',
];
foreach ($modules as $module) {
$batch['operations'][] = ['pco_cities_install_module', (array) $module];
}
return $batch;
}
/**
* Batch API callback. Installs a module.
*
* @param string|array $module
* The name(s) of the module(s) to install.
*/
function pco_cities_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 pco_cities_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();
// The directory where the language config files reside.
$language_config_directory = __DIR__ . '/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.
$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();
}
}
}