-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwa.module
91 lines (75 loc) · 2.19 KB
/
pwa.module
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
<?php
/**
* @file
* PWA hooks.
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
/**
* Implements hook_page_attachments().
*/
function pwa_page_attachments(array &$attachments) {
if (!\Drupal::currentUser()->hasPermission('access content')) {
return;
}
$attachments['#attached']['library'][] = 'pwa/serviceworker';
$config = \Drupal::config('pwa.config');
$cross_origin = $config->get('cross_origin');
$manifest_link = [
'#tag' => 'link',
'#attributes' => [
'rel' => 'manifest',
'href' => Url::fromRoute('pwa.manifest')->toString(),
],
];
// Pass credentials if the site is behind HTTP auth.
if ($cross_origin) {
$manifest_link['#attributes']['crossorigin'] = 'use-credentials';
}
$attachments['#attached']['html_head'][] = [$manifest_link, 'manifest'];
$theme_color = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'theme-color',
'content' => $config->get('theme_color'),
],
];
$attachments['#attached']['html_head'][] = [$theme_color, 'theme_color'];
/** @var callable */
$getCacheVersionCallable = \Drupal::service('controller_resolver')
->getControllerFromDefinition(
'\Drupal\pwa\Controller\PWAController::pwa_get_cache_version'
);
// Add cache version to drupal settings.
$attachments['#attached']['drupalSettings']['pwa']['cache_version'] =
\call_user_func($getCacheVersionCallable);
$installPath = '/serviceworker-pwa';
\Drupal::moduleHandler()->alter('pwa_install_path', $installPath);
$attachments['#attached']['drupalSettings']['pwa']['installPath'] = $installPath;
$scope = $config->get('scope');
$attachments['#attached']['drupalSettings']['pwa']['scope'] = !empty($scope) ? $scope : NULL;
}
/**
* Implements hook_theme().
*/
function pwa_theme() {
return [
'offline' => [
'variables' => [],
],
];
}
/**
* Check data from input.
*/
function pwa_str_to_list($string) {
$list = $string ? explode("\n", $string) : [];
$list = array_map('trim', $list);
return array_filter($list, 'strlen');
}
/**
* Implements hook_user_login().
*/
function pwa_user_login(AccountInterface $account) {
\Drupal::service('session')->set('pwa_reset', TRUE);
}