You are here

function pwa_extras_page_attachments in Progressive Web App 8

Same name and namespace in other branches
  1. 2.x modules/pwa_extras/pwa_extras.module \pwa_extras_page_attachments()

Implements hook_page_attachments().

File

modules/pwa_extras/pwa_extras.module, line 11
Contains pwa_extras.module

Code

function pwa_extras_page_attachments(array &$attachments) {
  if (!\Drupal::currentUser()
    ->hasPermission('access content')) {
    return;
  }
  $site_name = \Drupal::config('pwa.config')
    ->get('site_name') ?: \Drupal::config('system.site')
    ->get('name');
  $config = \Drupal::config('pwa_extras.settings.apple');
  $mask_color = $config
    ->get('mask_color') ?: '#0678be';
  $status_color = $config
    ->get('color_select') ?: 'default';
  $tag_list = pwa_extras_tag_list($site_name, $mask_color, $status_color);
  $meta_tags = $config
    ->get('touch_icons') + $config
    ->get('meta_tags') + $config
    ->get('home_screen_icons');
  if (empty($meta_tags)) {
    return;
  }
  foreach ($meta_tags as $key => $value) {
    if (!$value) {
      continue;
    }
    $string_tag = $tag_list[$key];
    $dom = new DOMDocument();
    $dom
      ->loadHTML($string_tag);
    $data = $dom->documentElement->firstChild->firstChild;
    $tag_name = $data->tagName;
    $tag_attributes = [];
    foreach ($data->attributes as $attribute) {
      $tag_attributes[$attribute->name] = $attribute->value;
    }
    if ($tag_name == 'link') {
      $attachments['#attached']['html_head_link'][] = [
        $tag_attributes,
      ];
    }
    else {
      $tag_data = [
        '#tag' => $tag_name,
        '#attributes' => $tag_attributes,
      ];
      $attachments['#attached']['html_head'][] = [
        $tag_data,
        $tag_attributes['name'],
      ];
    }
  }
}