forked from apigee/apigee-devportal-kickstart-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapigee_devportal_kickstart.install
597 lines (536 loc) · 19.6 KB
/
apigee_devportal_kickstart.install
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
* Copyright 2019 Google Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
use Apigee\Edge\Api\Management\Controller\OrganizationController;
use Drupal\apigee_devportal_kickstart\Installer\ApigeeDevportalKickstartTasksManager;
use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeEdgeConfigurationForm;
use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeMonetizationConfigurationForm;
use Drupal\apigee_devportal_kickstart\Installer\Form\DemoInstallForm;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\views\Views;
/**
* @file
* Install, update and uninstall functions for Apigee Kickstart profile.
*/
/**
* Implements hook_install_tasks().
*/
function apigee_devportal_kickstart_install_tasks(&$install_state) {
// Initialize the seven theme and set it as active.
// This ensures the installation theme is maintained across tasks.
$theme = \Drupal::service('theme.initialization')->initTheme('seven');
\Drupal::theme()->setActiveTheme($theme);
// This needs to run before m10n tasks.
$tasks = [
'apigee_devportal_monetization_preflight' => [],
];
// Add monetization tasks if the configured organization is monetizable.
if (Drupal::state()->get('is_monetizable')) {
$tasks[ApigeeMonetizationConfigurationForm::class] = [
'display_name' => t('Configure monetization'),
'type' => 'form',
];
// Setup monetization if all dependencies are met.
if (empty(Drupal::service('apigee_devportal_kickstart.monetization')->getMissingDependencies())) {
$tasks['apigee_devportal_setup_monetization'] = [
'display_name' => t('Setup monetization'),
'type' => 'batch',
];
}
}
// This runs after m10n tasks in case m10n needs to set default content.
$tasks[DemoInstallForm::class] = [
'display_name' => t('Install demo content'),
'type' => 'form',
];
$tasks['apigee_devportal_kickstart_theme_setup'] = [
'display_name' => t('Install theme'),
'display' => FALSE,
];
$tasks['apigee_devportal_kickstart_finish'] = [
'display' => FALSE,
];
return $tasks;
}
/**
* Implements hook_install_tasks_alter().
*/
function apigee_devportal_kickstart_install_tasks_alter(&$tasks, $install_state) {
// Do not add the apigee_edge_configure_form tasks if non-interactive install
// since drush si cannot set default values for the form.
// Use `drush key-save apigee_edge_connection_default '{\"auth_type\":\"basic\",\"organization\":\"ORGANIZATION\",\"username\":\"USERNAME\",\"password\":\"PASSWORD"}' --key-type=apigee_auth -y`
// to create a key after drush si.
if (!$install_state['interactive']) {
return;
}
// Add tasks for configuring Apigee authentication and monetization.
$apigee_kickstart_tasks = [
ApigeeEdgeConfigurationForm::class => [
'display_name' => t('Configure Apigee Edge'),
'type' => 'form',
],
];
// The task should run before install_configure_form which creates the user.
$tasks_copy = $tasks;
$tasks = array_slice($tasks_copy, 0, array_search('install_configure_form', array_keys($tasks))) + $apigee_kickstart_tasks + $tasks_copy;
}
/**
* Prepares profile for monetization setup.
*
* @param array $install_state
* The install state.
*/
function apigee_devportal_monetization_preflight(array &$install_state) {
// Check if monetization is enabled and persist the value in state.
if (Drupal::hasService('apigee_devportal_kickstart.monetization')) {
Drupal::state()->set('is_monetizable', Drupal::service('apigee_devportal_kickstart.monetization')->isMonetizable());
}
if (!\Drupal::moduleHandler()->moduleExists('address')) {
return;
}
// The monetization configuration form needs an address field.
// Enable the address module.
\Drupal::service('apigee_devportal_kickstart.monetization')->ensureInstalled('address');
}
/**
* Install task for setting up monetization and additional modules.
*
* @param array $install_state
* The install state.
*
* @return array
* A batch definition.
*/
function apigee_devportal_setup_monetization(array &$install_state) {
if (isset($install_state['m10n_config']) && ($config = $install_state['m10n_config'])) {
// Add an operations to install modules.
$operations = [
[
[ApigeeDevportalKickstartTasksManager::class, 'init'],
[$config]
],
[
[ApigeeDevportalKickstartTasksManager::class, 'installModules'],
[$config['modules']],
],
];
// Perform additional tasks for apigee_m10n_add_credit.
if (in_array('apigee_m10n_add_credit', $config['modules'])) {
$operations = array_merge($operations, [
[
[ApigeeDevportalKickstartTasksManager::class, 'importCurrencies'],
[$config['supported_currencies']],
],
[
[ApigeeDevportalKickstartTasksManager::class, 'createStore'],
[$config['store']],
],
[
[ApigeeDevportalKickstartTasksManager::class, 'createProductType'],
[$config],
],
[
[ApigeeDevportalKickstartTasksManager::class, 'createProducts'],
[$config['supported_currencies']],
],
]);
if (!empty($config['gateway'])) {
$operations[] = [
[ApigeeDevportalKickstartTasksManager::class, 'createPaymentGateway'],
[$config],
];
}
}
$batch = [
'operations' => $operations,
'title' => t('Setting up monetization'),
'error_message' => t('The installation has encountered an error.'),
'progress_message' => t('Completed @current out of @total tasks.'),
];
return $batch;
}
}
/**
* Install the theme.
*
* @param array $install_state
* The install state.
*/
function apigee_devportal_kickstart_theme_setup(array &$install_state) {
// Clear all status messages generated by modules installed in previous step.
Drupal::messenger()->deleteByType(MessengerInterface::TYPE_STATUS);
// Set apigee_kickstart as the default theme.
\Drupal::configFactory()
->getEditable('system.theme')
->set('default', 'apigee_kickstart')
->save();
// Ensure that the install profile's theme is used.
// @see _drupal_maintenance_theme()
\Drupal::service('theme.manager')->resetActiveTheme();
// Enable the admin theme for editing content.
\Drupal::configFactory()
->getEditable('node.settings')
->set('use_admin_theme', TRUE)
->save(TRUE);
}
/**
* Run any additional tasks for the installation.
*/
function apigee_devportal_kickstart_finish() {
// Re-run the optional config import again since Drupal installation profile
// imports optional configuration only once.
// @see \Drupal\Core\Config\ConfigInstaller::installDefaultConfig
// @see install_install_profile()
\Drupal::service('config.installer')->installOptionalConfig();
}
/**
* Implements hook_install().
*
* Perform actions to set up the site for this profile.
*
* @see system_install()
*/
function apigee_devportal_kickstart_install() {
// Get profile path.
$src_path = drupal_get_path('profile', 'apigee_devportal_kickstart') . '/resources/files';
// Get public files directory.
$dest_path = \Drupal::config('system.file')->get('default_scheme') . '://';
// Files to copy.
// Not all files are copied. So we list them here individually.
$filesets = [
'' => [
'aditya-vyas-1392552-unsplash.jpg',
'chuttersnap-255216-unsplash.png',
'donald-giannatti-671274-unsplash.jpg',
'nasa-43567-unsplash.jpg',
'nasa-43569-unsplash.jpg',
'nasa-89116-unsplash.jpg',
'spacex-1130896-unsplash.jpg',
'spacex-81773-unsplash.jpg',
'terence-burke-1417892-unsplash.jpg',
],
'default_images/' => [
'profile-default.jpg',
],
'apidoc_specs/' => [
'httpbin-simple-oas3.yaml',
],
];
// Copy files in public files directory. Note: Managed files are not desired.
foreach ($filesets as $destination => $files) {
foreach ($files as $file) {
if (file_exists($src_path . '/' . $file)) {
$file_system = \Drupal::service('file_system');
// If the file exists in public://, it should be skipped, as these are
// tied to media entities which hard-code the path/file.
$directory = $dest_path . $destination;
if (!empty($destination)) {
$file_system->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
}
try {
\Drupal::service('file_system')->copy($src_path . '/' . $file, $directory . $file);
}
catch (FileException $e) {
// Do nothing.
}
}
}
}
// Create a forum link.
// This cannot be added as config since uninstalling forum.module will result
// in a RouteNotFoundException.
// See https://github.com/apigee/apigee-devportal-kickstart-drupal/issues/276.
MenuLinkContent::create([
'title' => 'Forum',
'menu_name' => 'main',
'link' => ['uri' => 'internal:/forum'],
'weight' => 3,
])->save();
}
/**
* Implements hook_requirements().
*/
function apigee_devportal_kickstart_requirements($phase) {
$requirements = [];
// Drupal 8.8.x requires pathauto >=8.x-1.6.
// Make sure this requirement is met.
if ($pathauto = Drupal::service('extension.list.module')->get('pathauto')) {
$current_version = $pathauto->info['version'];
$required_version = '8.x-1.6';
if (!version_compare($current_version, $required_version, '>=')) {
$requirements['apigee_devportal_kickstart_pathauto_version'] = [
'title' => t('Apigee Devportal Kickstart'),
'description' => t('Apigee Devportal Kickstart requires <a href=":url">Pathauto</a> <em>@required_version</em>. The current version is <em>@current_version</em>. Please update to the required version to ensure future compatibility.', [
':url' => 'https://drupal.org/project/pathauto',
'@required_version' => $required_version,
'@current_version' => $current_version,
]),
'severity' => REQUIREMENT_ERROR,
];
}
}
// Check if the private file stream wrapper is ready to use for OAuth connection.
if (!\Drupal::hasService('stream_wrapper.private')) {
$requirements['apigee_devportal_kickstart_private_filesystem'] = [
'title' => t('Apigee Devportal Kickstart'),
'description' => t('The private file system must be configured prior to configuring the connection to Apigee and specifying authentication details. See <a href=":url">Configure the private file system path</a> for details.', [
':url' => 'https://www.drupal.org/docs/8/core/modules/file/overview#private-file-system',
]),
'severity' => REQUIREMENT_WARNING,
];
}
return $requirements;
}
/**
* Implements hook_update_dependencies().
*/
function apigee_devportal_kickstart_update_dependencies() {
$dependencies = [];
if (\Drupal::moduleHandler()->moduleExists('apigee_api_catalog')) {
// Make sure update 8007 is executed after apigee_api_catalog 8804.
$dependencies['apigee_devportal_kickstart'][8007] = [
'apigee_api_catalog' => 8804,
];
}
return $dependencies;
}
/**
* Replace 'Forum' menu link with one that can be edited.
*/
function apigee_devportal_kickstart_update_8001(&$sandbox) {
// Replaced apigee_devportal_kickstart.links.menu.yml with the following.
MenuLinkContent::create([
'title' => 'Forum',
'menu_name' => 'main',
'link' => ['uri' => 'internal:/forum'],
'weight' => 3,
])->save();
}
/**
* Disable deprecated module "apigee_kickstart_m10n_add_credit".
*/
function apigee_devportal_kickstart_update_8002(&$sandbox) {
try {
\Drupal::service('module_installer')->uninstall(['apigee_kickstart_m10n_add_credit']);
$context['message'] = t('Uninstalled "apigee_kickstart_m10n_add_credit" module.');
}
catch (\Exception $exception) {
watchdog_exception('apigee_kickstart', $exception);
}
}
/**
* Enable display settings for apps.
*/
function apigee_devportal_kickstart_update_8006(&$sandbox) {
$config_factory = \Drupal::configFactory();
foreach (\Drupal::service('apigee_kickstart.enhancer')->getAppEntityTypes() as $entity_type_id => $app_entity_type) {
$config_factory->getEditable("apigee_edge.display_settings.{$entity_type_id}")
->set('display_type', 'view_mode')
->set('view_mode', 'collapsible_card')
->save();
}
}
/**
* Update to apigee_api_catalog 2.x.
*/
function apigee_devportal_kickstart_update_8007() {
$moduleHandler = \Drupal::moduleHandler();
if (!$moduleHandler->moduleExists('apigee_api_catalog')) {
return;
}
$configPath = drupal_get_path('profile', 'apigee_devportal_kickstart') . '/config/install';
$configToImport = 'core.entity_view_mode.node.card';
$raw = file_get_contents("$configPath/$configToImport.yml");
$data = Yaml::decode($raw);
\Drupal::configFactory()
->getEditable($configToImport)
->setData($data)
->save(TRUE);
if ($moduleHandler->moduleExists('views')) {
$configToImport = 'views.view.apigee_api_catalog';
$raw = file_get_contents("$configPath/$configToImport.yml");
$data = Yaml::decode($raw);
\Drupal::configFactory()
->getEditable($configToImport)
->setData($data)
->save(TRUE);
}
}
/**
* Fix image and categories fields for api_doc.
*/
function apigee_devportal_kickstart_update_8008() {
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
$entity_field_manager = \Drupal::service('entity_field.manager');
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $definitions */
$definitions = $entity_field_manager->getFieldDefinitions('node', 'apidoc');
/** @var \Drupal\Core\Entity\EntityDisplayRepository $entity_display_repository */
$entity_display_repository = \Drupal::service('entity_display.repository');
/** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $entity_form_display */
$entity_form_display = $entity_display_repository->getFormDisplay('node', 'apidoc');
$entity_view_display = $entity_display_repository->getViewDisplay('node', 'apidoc', 'card');
$fields = [
'field_image_apidoc' => [
'old_label' => 'apidoc.field_image',
'label' => 'Image',
'form_display' => [
'type' => 'media_library_widget',
'region' => 'content',
],
'view_display' => [
'label' => 'hidden',
'type' => 'entity_reference_entity_view',
'region' => 'content',
'settings' => [
'view_mode' => 'card_small',
'link' => false,
],
],
],
'field_categories_apidoc' => [
'old_label' => 'apidoc.field_categories',
'label' => 'Categories',
'form_display' => [
'type' => 'entity_reference_autocomplete',
'region' => 'content',
],
],
];
foreach ($fields as $field_name => $field_config) {
if (isset($definitions[$field_name])) {
$api_field_config = $definitions[$field_name]->getConfig('apidoc');
// Update the label.
if ($api_field_config->getLabel() === $field_config['old_label']) {
$definitions[$field_name]->getConfig('apidoc')->setLabel($field_config['label'])->save();
}
// Enable the field on the form display.
if (!$entity_form_display->getComponent($field_name)) {
$entity_form_display->setComponent($field_name, $field_config['form_display'])->save();
}
// Update the field on the view display.
if (!$entity_view_display->getComponent($field_name) && !empty($field_config['view_display'])) {
$entity_view_display->setComponent($field_name, $field_config['view_display'])->save();
}
}
}
}
/**
* Fix API Catalog view.
*/
function apigee_devportal_kickstart_update_8009() {
$view = Views::getView('apigee_api_catalog');
if(is_object($view)){
$display = $view->getDisplay();
$filters = $display->getOption('filters');
// Remove the broken filter.
if (!empty($filters['field_categories_target_id'])) {
unset($filters['field_categories_target_id']);
}
// Add new "field_categories_apidoc" field as filter.
if (empty($filters['field_categories_apidoc_target_id'])) {
$filters['field_categories_apidoc_target_id'] = [
'id' => 'field_categories_apidoc_target_id',
'table' => 'node__field_categories_apidoc',
'field' => 'field_categories_apidoc_target_id',
'relationship' => 'none',
'group_type' => 'group',
'admin_label' => '',
'operator' => 'or',
'value' => [],
'group' => 1,
'exposed' => TRUE,
'expose' => [
'operator_id' => 'field_categories_apidoc_target_id_op',
'label' => 'Categories',
'description' => '',
'use_operator' => FALSE,
'operator' => 'field_categories_apidoc_target_id_op',
'operator_limit_selection' => FALSE,
'operator_list' => [],
'identifier' => 'field_categories_target_id',
'required' => FALSE,
'remember' => FALSE,
'multiple' => TRUE,
'remember_roles' => [
'authenticated' => 'authenticated',
'anonymous' => '0',
'administrator' => '0',
],
'reduce' => FALSE
],
'is_grouped' => FALSE,
'group_info' => [
'label' => '',
'description' => '',
'identifier' => '',
'optional' => TRUE,
'widget' => 'select',
'multiple' => FALSE,
'remember' => FALSE,
'default_group' => 'All',
'default_group_multiple' => [],
'group_items' => [],
],
'reduce_duplicates' => FALSE,
'type' => 'select',
'limit' => TRUE,
'vid' => 'api_category',
'hierarchy' => TRUE,
'error_message' => TRUE,
'plugin_id' => 'taxonomy_index_tid'
];
// Update the exposed form.
$exposed_form = $display->getOption('exposed_form');
$exposed_form['options']['bef']['filter']['field_categories_apidoc_target_id'] = [
'plugin_id' => 'bef',
'advanced' => [
'rewrite' => [
'filter_rewrite_values' => '',
],
'collapsible' => FALSE,
'is_secondary' => FALSE,
],
'select_all_none' => FALSE,
'select_all_none_nested' => FALSE,
'display_inline' => 0
];
$display->setOption('exposed_form', $exposed_form);
}
$display->setOption('filters', $filters);
$view->save();
}
}
/**
* Enable warnings field in developer_app collapsible_card view display.
*/
function apigee_devportal_kickstart_update_8010() {
/** @var \Drupal\Core\Entity\EntityDisplayRepository $entity_display_repository */
$entity_display_repository = \Drupal::service('entity_display.repository');
$entity_view_display = $entity_display_repository->getViewDisplay('developer_app', 'developer_app', 'collapsible_card');
if ($entity_view_display->getComponent('warnings')) {
return;
}
$entity_view_display->setComponent('warnings', [
'region' => 'content',
'weight' => -100,
])->save();
}