-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwa.install
110 lines (98 loc) · 3.2 KB
/
pwa.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
<?php
/**
* @file
* PWA install hooks.
*/
use Drupal\user\RoleInterface;
/**
* Implements hook_requirements().
*/
function pwa_requirements($phase) {
$requirements = [];
if ($phase !== 'runtime') {
return $requirements;
}
$t = 't';
if ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') || (isset($_SERVER["REQUEST_SCHEME"]) && $_SERVER["REQUEST_SCHEME"] === 'https')) {
$requirements['pwa'] = [
'title' => $t('Progressive Web App'),
'value' => $t('HTTPS on'),
'severity' => REQUIREMENT_OK,
'description' => $t('Please make sure the certificate of %domain is valid for offline functionality to work.', ['%domain' => $_SERVER['HTTP_HOST']]),
];
}
elseif (in_array($_SERVER['HTTP_HOST'], ['localhost', '127.0.0.1'])) {
$requirements['pwa'] = [
'title' => $t('Progressive Web App'),
'value' => 'localhost',
'severity' => REQUIREMENT_WARNING,
'description' => $t('You will need to configure HTTPS on your domain for this module to work.'),
];
}
else {
$requirements['pwa'] = [
'title' => $t('Progressive Web App'),
'value' => $t('HTTPS off'),
'severity' => REQUIREMENT_ERROR,
'description' => $t('HTTPS need to be configured for the progressive web app module to work.'),
];
}
return $requirements;
}
/**
* Implements hook_install().
*/
function pwa_install() {
// Set the site name dynamically.
$site_name = \Drupal::config('system.site')->get('name');
$output = [
'site_name' => $site_name,
'short_name' => substr($site_name, 0, 25),
];
$config = \Drupal::configFactory()->getEditable('pwa.config');
foreach ($output as $key => $value) {
$config->set($key, $value);
}
$config->save();
}
/**
* Implements hook_uninstall().
*/
function pwa_uninstall() {
// Remove the pwa images.
\Drupal::service('pwa.manifest')->deleteImage();
// Remove the pwa configuration.
\Drupal::configFactory()->getEditable('pwa.config')->delete();
}
/**
* Set the default offline_page variable.
*/
function pwa_update_8001(&$sandbox) {
// Set the offline page path to the default value.
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('pwa.config');
$config->set('offline_page', '/offline')->save(TRUE);
// Remove the offline page path from the URL's to cache. It will be added
// automatically in PWAController::pwa_serviceworker_file_data().
$cache_urls = array_diff(pwa_str_to_list($config->get('urls_to_cache')), ['/offline']);
$config->set('urls_to_cache', implode("\r\n", $cache_urls))->save(TRUE);
}
/**
* Set default scope to "/".
*/
function pwa_update_8002() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('pwa.config');
$config->set('scope', '/')
->save(TRUE);
// Delete the cached config so that Drupal starts using the new config. Note
// that invalidating this cache ID doesn't seem to do it for whatever reason,
// so we have to actually delete it.
\Drupal::cache('config')->delete('pwa.config');
}
/**
* Remove the non-existent 'access pwa' permission from the anonymous user role.
*/
function pwa_update_8003() {
user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access pwa']);
}