-
Notifications
You must be signed in to change notification settings - Fork 0
/
drustack.profile
296 lines (257 loc) · 9.34 KB
/
drustack.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
<?php
/**
* @file
* Enables modules and site configuration for a drustack site installation.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\features\ConfigurationItem;
use Drupal\features\FeaturesManagerInterface;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
/**
* Implements hook_install_tasks().
*/
function drustack_install_tasks($install_state) {
$modules = [
'drustack_core',
'drustack_devel',
'drustack_seo',
'drustack_performance',
'drustack_wysiwyg',
'drustack_paragraphs',
'drustack_layout',
'drustack_page',
'drustack_article',
'drustack_blog',
'drustack_webform',
];
\Drupal::state()->set('drustack_features_exports', $modules);
return [
'_drustack_features_install' => [
'display_name' => t('Install features'),
'type' => 'batch',
],
'_drustack_features_import_all' => [
'display_name' => t('Import features'),
'type' => 'batch',
],
'_drustack_configure_cleanup' => [],
];
}
/**
* Implements hook_form_FORM_ID_alter() for install_configure_form().
*
* Allows the profile to alter the site configuration form.
*/
function drustack_form_install_configure_form_alter(&$form, FormStateInterface $form_state) {
// Clear drupal message queue for non-errors.
\Drupal::service('messenger')->deleteByType(MessengerInterface::TYPE_STATUS);
\Drupal::service('messenger')->deleteByType(MessengerInterface::TYPE_WARNING);
// Site information form.
$form['site_information']['#weight'] = -20;
$form['site_information']['site_name']['#default_value'] = \Drupal::request()->server->get('SERVER_NAME');
$form['site_information']['site_mail']['#default_value'] = '[email protected]';
// Administrator account form.
$form['admin_account']['#weight'] = -19;
$form['admin_account']['account']['name']['#default_value'] = 'admin';
$form['admin_account']['account']['mail']['#default_value'] = '[email protected]';
// Power user account form.
$form['webmaster_account'] = [
'#type' => 'fieldgroup',
'#title' => t('Site power user account'),
'#collapsible' => FALSE,
];
$form['webmaster_account']['#weight'] = -18;
$form['webmaster_account']['webmaster_account']['#tree'] = TRUE;
$form['webmaster_account']['webmaster_account']['name'] = [
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => 'webmaster',
'#maxlength' => UserInterface::USERNAME_MAX_LENGTH,
'#description' => t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
'#required' => TRUE,
'#attributes' => ['class' => ['username']],
];
$form['webmaster_account']['webmaster_account']['mail'] = [
'#type' => 'email',
'#title' => t('Email address'),
'#default_value' => '[email protected]',
'#required' => TRUE,
];
// Just alter the weight.
$form['regional_settings']['#weight'] = -17;
$form['update_notifications']['#weight'] = -16;
$form['actions']['#weight'] = -15;
// Add our own validation.
$form['#validate'][] = '_drustack_form_install_configure_form_validate';
// Add our own submit handler.
$form['#submit'][] = '_drustack_form_install_configure_form_submit';
}
/**
* Validate Power user account.
*/
function _drustack_form_install_configure_form_validate($form, FormStateInterface $form_state) {
// Check admin account.
if ($error = user_validate_name($form_state->getValue(['webmaster_account', 'name']))) {
$form_state->setErrorByName('webmaster_account][name', $error);
}
if ($form_state->getValue(['webmaster_account', 'name']) == $form_state->getValue(['account', 'name'])) {
$form_state->setErrorByName('webmaster_account][name', t('The admin name is not valid.'));
}
if ($form_state->getValue(['webmaster_account', 'mail']) == $form_state->getValue(['account', 'mail'])) {
$form_state->setErrorByName('webmaster_account][mail', t('The admin email address is not valid.'));
}
}
/**
* Create Power user account and change root password.
*/
function _drustack_form_install_configure_form_submit($form, FormStateInterface $form_state) {
// Add the user and associate role here.
$role = Role::load('power');
if (!$role) {
$role = Role::create([
'id' => 'power',
'label' => 'Power user',
]);
$role->save();
}
// We keep power user and administrator account password in sync by default.
$account = User::create([
'mail' => $form_state->getValue(['webmaster_account', 'mail']),
'name' => $form_state->getValue(['webmaster_account', 'name']),
'pass' => $form_state->getValue(['account', 'pass']),
'init' => $form_state->getValue(['webmaster_account', 'mail']),
'status' => 1,
'roles' => [$role],
]);
$account->save();
}
/**
* Install features exports.
*
* @return
* The batch definition.
*
* @see install_profile_modules()
*/
function _drustack_features_install(&$install_state) {
$features = \Drupal::state()->get('drustack_features_exports') ?: [];
$files = \Drupal::service('extension.list.module')->getList();
// Always install required modules first. Respect the dependencies between
// the modules.
$required = [];
$non_required = [];
// Add modules that other modules depend on.
$modules = [];
foreach ($features 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);
$features = array_flip($features);
$operations = [];
foreach ($required + $non_required + $features 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.'),
];
return $batch;
}
/**
* Import all features exports.
*
* @return
* The batch definition.
*
* @see drush_features_import_all()
*/
function _drustack_features_import_all(&$install_state) {
$manager = \Drupal::service('features.manager');
$packages = $manager->getPackages();
$packages = $manager->filterPackages($packages);
$operations = [];
foreach ($packages as $package) {
$overrides = $manager->detectOverrides($package);
if ($package->getStatus() == FeaturesManagerInterface::STATUS_INSTALLED) {
$operations[] = ['_drustack_features_import', [$package->getMachineName(), $package->getName()]];
}
}
$batch = [
'operations' => $operations,
'title' => t('Importing @drupal', ['@drupal' => drupal_install_profile_distribution_name()]),
'error_message' => t('The installation has encountered an error.'),
];
return $batch;
}
/**
* Revert features exports.
*
* @see drush_features_export()
*/
function _drustack_features_import($module, $module_name, &$context) {
$context['results'][] = $module;
$manager = \Drupal::service('features.manager');
$config_revert = \Drupal::service('features.config_update');
$module_handler = \Drupal::moduleHandler();
$modules = $module_handler->getModuleList();
if (!empty($modules[$module])) {
$extension = $modules[$module];
$feature = $manager->initPackageFromExtension($extension);
$feature->setConfig($manager->listExtensionConfig($extension));
$feature->setStatus(FeaturesManagerInterface::STATUS_INSTALLED);
$components = $feature->getConfigOrig();
foreach ($components as $component) {
if (!isset($config[$component])) {
$item = $manager->getConfigType($component);
$type = ConfigurationItem::fromConfigStringToConfigType($item['type']);
$config_revert->import($type, $item['name_short']);
$context['message'] = t('Import %module : %component.', ['%module' => $module_name, '%component' => $component]);
}
else {
$item = $config[$component];
$type = ConfigurationItem::fromConfigStringToConfigType($item->getType());
$config_revert->revert($type, $item->getShortName());
$context['message'] = t('Revert %module : %component.', ['%module' => $module_name, '%component' => $component]);
}
}
}
}
/**
* Various actions needed to clean up after the installation.
*/
function _drustack_configure_cleanup() {
// Clear the APC cache to ensure APC class loader is reset.
if (function_exists('apc_fetch')) {
apc_clear_cache('user');
}
// Flush all existing path aliases.
\Drupal::service('database')->delete('url_alias');
// Rebuild node access database.
node_access_rebuild();
// Rebuild the menu.
\Drupal::service('router.builder')->rebuild();
// Clear out caches.
drupal_flush_all_caches();
// Clear out JS and CSS caches.
\Drupal::service('asset.css.collection_optimizer')->deleteAll();
\Drupal::service('asset.js.collection_optimizer')->deleteAll();
// Clear drupal message queue for non-errors.
\Drupal::service('messenger')->deleteByType(MessengerInterface::TYPE_STATUS);
\Drupal::service('messenger')->deleteByType(MessengerInterface::TYPE_WARNING);
}