You are here

function pwa_page_attachments in Progressive Web App 8

Same name and namespace in other branches
  1. 2.x pwa.module \pwa_page_attachments()

Implements hook_page_attachments().

File

./pwa.module, line 14
PWA hooks.

Code

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;
}