forked from goalgorilla/open_social
-
Notifications
You must be signed in to change notification settings - Fork 0
/
social.profile
597 lines (524 loc) · 19.3 KB
/
social.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
<?php
/**
* @file
* Enables modules and site configuration for a social site installation.
*/
use Drupal\user\Entity\User;
use Drupal\Core\Form\FormStateInterface;
use Drupal\features\FeaturesManagerInterface;
use Drupal\features\ConfigurationItem;
use Drupal\search_api\Entity\Index;
/**
* Implements hook_install_tasks().
*/
function social_install_tasks(&$install_state) {
$tasks = [
'social_install_profile_modules' => [
'display_name' => t('Install Open Social modules'),
'type' => 'batch',
],
'social_final_site_setup' => [
'display_name' => t('Apply configuration'),
'type' => 'batch',
'display' => TRUE,
],
'social_theme_setup' => [
'display_name' => t('Apply theme'),
'display' => TRUE,
],
];
return $tasks;
}
/**
* Implements hook_install_tasks_alter().
*
* Unfortunately we have to alter the verify requirements.
* This is because of https://www.drupal.org/node/1253774. The dependencies of
* dependencies are not tested. So adding requirements to our install profile
* hook_requirements will not work :(. Also take a look at install.inc function
* drupal_check_profile() it just checks for all the dependencies of our
* install profile from the info file. And no actually hook_requirements in
* there.
*/
function social_install_tasks_alter(&$tasks, $install_state) {
// Override the core install_verify_requirements task function.
$tasks['install_verify_requirements']['function'] = 'social_verify_custom_requirements';
// Override the core finished task function.
$tasks['install_finished']['function'] = 'social_install_finished';
}
/**
* Callback for install_verify_requirements, so that we meet custom requirement.
*
* @param array $install_state
* The current install state.
*
* @return array
* All the requirements we need to meet.
*/
function social_verify_custom_requirements(array &$install_state) {
// Copy pasted from install_verify_requirements().
// @todo when composer hits remove this.
// Check the installation requirements for Drupal and this profile.
$requirements = install_check_requirements($install_state);
// Verify existence of all required modules.
$requirements += drupal_verify_profile($install_state);
// Added a custom check for users to see if the Address libraries are
// downloaded.
if (!class_exists('\CommerceGuys\Addressing\Address')) {
$requirements['addressing_library'] = [
'title' => t('Address module requirements)'),
'value' => t('Not installed'),
'description' => t('The Address module requires the commerceguys/addressing library. <a href=":link" target="_blank">For more information check our readme</a>', [':link' => 'https://github.com/goalgorilla/drupal_social/blob/master/readme.md#install-from-project-page-on-drupalorg']),
'severity' => REQUIREMENT_ERROR,
];
}
if (!class_exists('\Facebook\Facebook')) {
$requirements['social_auth_facebook'] = [
'title' => t('Social Auth Facebook module requirements'),
'value' => t('Not installed'),
'description' => t('Social Auth Facebook requires Facebook PHP Library. Make sure the library is installed via Composer.'),
'severity' => REQUIREMENT_ERROR,
];
}
if (!class_exists('\Google_Client')) {
$requirements['social_auth_google'] = [
'title' => t('Social Auth Google module requirements'),
'value' => t('Not installed'),
'description' => t('Social Auth Google requires Google_Client PHP Library. Make sure the library is installed via Composer.'),
'severity' => REQUIREMENT_ERROR,
];
}
if (!class_exists('\Happyr\LinkedIn\LinkedIn')) {
$requirements['social_auth_linkedin'] = [
'title' => t('Social Auth LinkedIn module requirements'),
'value' => t('Not installed'),
'description' => t('Social Auth LinkedIn requires LinkedIn PHP Library. Make sure the library is installed via Composer.'),
'severity' => REQUIREMENT_ERROR,
];
}
if (!class_exists('\Abraham\TwitterOAuth\TwitterOAuth')) {
$requirements['social_auth_twitter'] = [
'title' => t('Social Auth Twitter module requirements'),
'value' => t('Not installed'),
'description' => t('Social Auth Twitter requires TwitterOAuth PHP Library. Make sure the library is installed via Composer.'),
'severity' => REQUIREMENT_ERROR,
];
}
return install_display_requirements($install_state, $requirements);
}
/**
* Implements hook_form_FORM_ID_alter() for install_configure_form().
*
* Allows the profile to alter the site configuration form.
*/
function social_form_install_configure_form_alter(&$form, FormStateInterface $form_state) {
// Add 'Social' fieldset and options.
$form['social'] = [
'#type' => 'fieldgroup',
'#title' => t('Open Social optional configuration'),
'#description' => t('All the required modules and configuration will be automatically installed and imported. You can optionally select additional features or generated demo content.'),
'#weight' => 50,
];
$social_optional_modules = [
'social_book' => t('Book functionality'),
'social_sharing' => t('Share content on social media'),
'social_event_type' => t('Categorize events in event types'),
'social_sso' => t('Registration with social networks'),
'social_file_private' => t('Use the private file system for uploaded files (highly recommended)'),
];
// Checkboxes to enable Optional modules.
$form['social']['optional_modules'] = [
'#type' => 'checkboxes',
'#title' => t('Enable additional features'),
'#options' => $social_optional_modules,
'#default_value' => [
'social_file_private',
],
];
// Checkboxes to generate demo content.
$form['social']['demo_content'] = [
'#type' => 'checkbox',
'#title' => t('Generate demo content and users'),
'#description' => t('Will generate files, users, groups, events, topics, comments and posts.'),
];
// Submit handler to enable features.
$form['#submit'][] = 'social_features_submit';
}
/**
* Submit handler.
*/
function social_features_submit($form_id, &$form_state) {
$optional_modules = array_filter($form_state->getValue('optional_modules'));
\Drupal::state()->set('social_install_optional_modules', $optional_modules);
\Drupal::state()->set('social_install_demo_content', $form_state->getValue('demo_content'));
}
/**
* Installs required modules via a batch process.
*
* @param array $install_state
* An array of information about the current installation state.
*
* @return array
* The batch definition.
*/
function social_install_profile_modules(array &$install_state) {
$files = system_rebuild_module_data();
$modules = [
'social_core' => 'social_core',
'social_user' => 'social_user',
'social_group' => 'social_group',
'social_event' => 'social_event',
'social_topic' => 'social_topic',
'social_profile' => 'social_profile',
'social_editor' => 'social_editor',
'social_comment' => 'social_comment',
'social_post' => 'social_post',
'social_page' => 'social_page',
'social_search' => 'social_search',
'social_activity' => 'social_activity',
'social_follow_content' => 'social_follow_content',
'social_mentions' => 'social_mentions',
'social_font' => 'social_font',
'social_like' => 'social_like',
'social_post_photo' => 'social_post_photo',
'social_swiftmail' => 'social_swiftmail',
];
$social_modules = $modules;
// 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) {
$module_requires = array_keys($files[$module]->requires);
// Remove the social modules from required modules.
$module_requires = array_diff_key($module_requires, $social_modules);
$modules = array_merge($modules, $module_requires);
}
}
$modules = array_unique($modules);
// Remove the social modules from to install modules.
$modules = array_diff_key($modules, $social_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);
$operations = [];
foreach ($required + $non_required + $social_modules as $module => $weight) {
$operations[] = [
'_social_install_module_batch',
[[$module], $module],
];
}
$batch = [
'operations' => $operations,
'title' => t('Install Open Social modules'),
'error_message' => t('The installation has encountered an error.'),
];
return $batch;
}
/**
* Final setup of Social profile.
*
* @param array $install_state
* The install state.
*
* @return array
* Batch settings.
*/
function social_final_site_setup(array &$install_state) {
// Clear all status messages generated by modules installed in previous step.
drupal_get_messages('status', TRUE);
// There is no content at this point.
node_access_needs_rebuild(FALSE);
$batch = [];
$social_optional_modules = \Drupal::state()->get('social_install_optional_modules');
foreach ($social_optional_modules as $module => $module_name) {
$batch['operations'][] = [
'_social_install_module_batch',
[[$module], $module_name],
];
}
$demo_content = \Drupal::state()->get('social_install_demo_content');
if ($demo_content === 1) {
$batch['operations'][] = [
'_social_install_module_batch',
[['social_demo'], 'social_demo'],
];
// Generate demo content.
$demo_content_types = [
'file' => t('files'),
'user' => t('users'),
'group' => t('groups'),
'topic' => t('topics'),
'event' => t('events'),
'event_enrollment' => t('event enrollments'),
'post' => t('posts'),
'comment' => t('comments'),
'like' => t('likes'),
// @todo Add 'event_type' if module is enabled.
];
foreach ($demo_content_types as $demo_type => $demo_description) {
$batch['operations'][] = [
'_social_add_demo_batch',
[$demo_type, $demo_description],
];
}
// Uninstall social_demo.
$batch['operations'][] = [
'_social_uninstall_module_batch',
[['social_demo'], 'social_demo'],
];
}
// Add some finalising steps.
$final_batched = [
'profile_weight' => t('Set weight of profile.'),
'router_rebuild' => t('Rebuild router.'),
'trigger_sapi_index' => t('Index search'),
'cron_run' => t('Run cron.'),
'import_optional_config' => t('Import optional configuration'),
];
foreach ($final_batched as $process => $description) {
$batch['operations'][] = [
'_social_finalise_batch',
[$process, $description],
];
}
return $batch;
}
/**
* Install the theme.
*
* @param array $install_state
* The install state.
*/
function social_theme_setup(array &$install_state) {
// Clear all status messages generated by modules installed in previous step.
drupal_get_messages('status', TRUE);
// Also install improved theme settings & color module, because it improves
// the social blue theme settings page.
$modules = ['color'];
\Drupal::service('module_installer')->install($modules);
$themes = ['socialblue'];
\Drupal::service('theme_handler')->install($themes);
\Drupal::configFactory()
->getEditable('system.theme')
->set('default', 'socialblue')
->save();
// Ensure that the install profile's theme is used.
// @see _drupal_maintenance_theme()
\Drupal::service('theme.manager')->resetActiveTheme();
$modules = ['improved_theme_settings'];
\Drupal::service('module_installer')->install($modules);
}
/**
* Performs final installation steps and displays a 'finished' page.
*
* @param array $install_state
* An array of information about the current installation state.
*
* @see install_finished()
*/
function social_install_finished(array &$install_state) {
// Clear all status messages generated by modules installed in previous step.
drupal_get_messages('status', TRUE);
if ($install_state['interactive']) {
// Load current user and perform final login tasks.
// This has to be done after drupal_flush_all_caches()
// to avoid session regeneration.
$account = User::load(1);
user_login_finalize($account);
}
}
/**
* Implements callback_batch_operation().
*
* Performs batch installation of modules.
*/
function _social_install_module_batch($module, $module_name, &$context) {
set_time_limit(0);
\Drupal::service('module_installer')->install($module);
$context['results'][] = $module;
$context['message'] = t('Install %module_name module.', ['%module_name' => $module_name]);
}
/**
* Implements callback_batch_operation().
*
* Performs batch uninstallation of modules.
*/
function _social_uninstall_module_batch($module, $module_name, &$context) {
set_time_limit(0);
\Drupal::service('module_installer')->uninstall($module);
$context['results'][] = $module;
$context['message'] = t('Uninstalled %module_name module.', ['%module_name' => $module_name]);
}
/**
* Implements callback_batch_operation().
*
* Performs batch demo content generation.
*/
function _social_add_demo_batch($demo_type, $demo_description, &$context) {
set_time_limit(0);
$num_created = 0;
$content_types = [$demo_type];
$manager = \Drupal::service('plugin.manager.demo_content');
$plugins = $manager->createInstances($content_types);
/** @var \Drupal\social_demo\DemoContentInterface $plugin */
foreach ($plugins as $plugin) {
$plugin->createContent();
$num_created = $plugin->count();
}
$context['results'][] = $demo_type;
$context['message'] = t('Generated %num %demo_description.', ['%num' => $num_created, '%demo_description' => $demo_description]);
}
/**
* Implements callback_batch_operation().
*
* Performs batch finalising.
*/
function _social_finalise_batch($process, $description, &$context) {
switch ($process) {
case 'profile_weight':
$profile = drupal_get_profile();
// Installation profiles are always loaded last.
module_set_weight($profile, 1000);
break;
case 'router_rebuild':
// Build the router once after installing all modules.
// This would normally happen upon KernelEvents::TERMINATE, but since the
// installer does not use an HttpKernel, that event is never triggered.
\Drupal::service('router.builder')->rebuild();
break;
case 'trigger_sapi_index':
$indexes = Index::loadMultiple();
/** @var \Drupal\search_api\Entity\Index $index */
foreach ($indexes as $index) {
$index->reindex();
}
break;
case 'cron_run':
// Run cron to populate update status tables (if available) so that users
// will be warned if they've installed an out of date Drupal version.
// Will also trigger indexing of profile-supplied content or feeds.
\Drupal::service('cron')->run();
break;
case 'import_optional_config':
// We need to import all the optional configuration as well, since
// this is not supported by Drupal Core installation profiles yet.
/** @var \Drupal\features\FeaturesAssignerInterface $assigner */
$assigner = \Drupal::service('features_assigner');
$bundle = $assigner->applyBundle('social');
if ($bundle->getMachineName() === 'social') {
$current_bundle = $bundle;
/** @var \Drupal\features\FeaturesManagerInterface $manager */
$manager = \Drupal::service('features.manager');
$packages = $manager->getPackages();
$packages = $manager->filterPackages($packages, $current_bundle->getMachineName());
$options = [];
foreach ($packages as $package) {
if ($package->getStatus() != FeaturesManagerInterface::STATUS_NO_EXPORT) {
$missing = $manager->reorderMissing($manager->detectMissing($package));
$overrides = $manager->detectOverrides($package, TRUE);
if (!empty($overrides) || !empty($missing)) {
$options += [
$package->getMachineName() => [],
];
}
}
}
/** @var \Drupal\features\FeaturesManagerInterface $manager */
$manager = \Drupal::service('features.manager');
$packages = $manager->getPackages();
$packages = $manager->filterPackages($packages, 'social');
$overridden = [];
foreach ($packages as $package) {
$overrides = $manager->detectOverrides($package);
$missing = $manager->detectMissing($package);
if ((!empty($missing) || !empty($overrides)) && ($package->getStatus() == FeaturesManagerInterface::STATUS_INSTALLED)) {
$overridden[] = $package->getMachineName();
}
}
if (!empty($overridden)) {
social_features_import($overridden);
}
}
break;
}
$context['results'][] = $process;
$context['message'] = $description;
}
/**
* Imports module config into the active store.
*
* @see drush_features_import()
*/
function social_features_import($args) {
/** @var \Drupal\features\FeaturesManagerInterface $manager */
$manager = \Drupal::service('features.manager');
/** @var \Drupal\config_update\ConfigRevertInterface $config_revert */
$config_revert = \Drupal::service('features.config_update');
// Parse list of arguments.
$modules = [];
foreach ($args as $arg) {
$arg = explode(':', $arg);
$module = array_shift($arg);
$component = array_shift($arg);
if (isset($module)) {
if (empty($component)) {
// If we received just a feature name, this means that we need all of
// its components.
$modules[$module] = TRUE;
}
elseif ($modules[$module] !== TRUE) {
if (!isset($modules[$module])) {
$modules[$module] = [];
}
$modules[$module][] = $component;
}
}
}
// Process modules.
foreach ($modules as $module => $components_needed) {
/** @var \Drupal\features\Package $feature */
$feature = $manager->loadPackage($module, TRUE);
if (empty($feature)) {
return;
}
if ($feature->getStatus() != FeaturesManagerInterface::STATUS_INSTALLED) {
return;
}
// Only revert components that are detected to be Overridden.
$components = $manager->detectOverrides($feature);
$missing = $manager->reorderMissing($manager->detectMissing($feature));
// Be sure to import missing components first.
$components = array_merge($missing, $components);
if (!empty($components_needed) && is_array($components_needed)) {
$components = array_intersect($components, $components_needed);
}
if (!empty($components)) {
$config = $manager->getConfigCollection();
foreach ($components as $component) {
if (!isset($config[$component])) {
// Import missing component.
/** @var array $item */
$item = $manager->getConfigType($component);
$type = ConfigurationItem::fromConfigStringToConfigType($item['type']);
$config_revert->import($type, $item['name_short']);
}
else {
// Revert existing component.
/** @var \Drupal\features\ConfigurationItem $item */
$item = $config[$component];
$type = ConfigurationItem::fromConfigStringToConfigType($item->getType());
$config_revert->revert($type, $item->getShortName());
}
}
}
}
}