pwa.install in Progressive Web App 8
Same filename and directory in other branches
PWA install hooks.
File
pwa.installView source
<?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() {
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'access pwa',
]);
// 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);
}
Functions
Name | Description |
---|---|
pwa_install | Implements hook_install(). |
pwa_requirements | Implements hook_requirements(). |
pwa_uninstall | Implements hook_uninstall(). |
pwa_update_8001 | Set the default offline_page variable. |