You are here

pwa.module in Progressive Web App 8

Same filename and directory in other branches
  1. 7.2 pwa.module
  2. 7 pwa.module
  3. 2.x pwa.module

PWA hooks.

File

pwa.module
View source
<?php

/**
 * @file
 * PWA hooks.
 */
use Drupal\Core\Url;
use Drupal\pwa\Controller\PWAController;

/**
 * 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',
  ];

  // Add cache version to drupal settings.
  $attachments['#attached']['drupalSettings']['pwa']['cache_version'] = PWAController::pwa_get_cache_version();
  $installPath = '/serviceworker-pwa';
  \Drupal::moduleHandler()
    ->alter('pwa_install_path', $installPath);
  $attachments['#attached']['drupalSettings']['pwa']['installPath'] = $installPath;
}

/**
 * Implements hook_theme().
 */
function pwa_theme() {
  return [
    'offline' => [
      'variables' => [],
    ],
  ];
}

/**
 * Check data from input.
 */
function pwa_str_to_list($string) {
  $list = explode("\n", $string);
  $list = array_map('trim', $list);
  return array_filter($list, 'strlen');
}

Functions

Namesort descending Description
pwa_page_attachments Implements hook_page_attachments().
pwa_str_to_list Check data from input.
pwa_theme Implements hook_theme().