You are here

function viewport_page_attachments_alter in Viewport 8

Implements hook_page_attachments_alter().

See also

system_page_attachments()

File

./viewport.module, line 86
Viewport module.

Code

function viewport_page_attachments_alter(array &$attachments) {

  // Use the viewport resolver service to see if a custom viewport is needed,
  // and to generate it.
  $viewport_resolver = Drupal::service('viewport.resolver');
  if ($viewport_resolver
    ->isPathSelected()) {
    $viewport_tag = $viewport_resolver
      ->generateViewportTagArray();

    // Replace core's viewport values with our custom values.
    // $head_array structure is "[$value, $key]" (e.g: [array(), 'viewport']).
    foreach ($attachments['#attached']['html_head'] as $index => $head_array) {
      if ($head_array[1] == 'viewport') {
        $attachments['#attached']['html_head'][$index][0] = $viewport_tag;
        $viewport_added = TRUE;
      }
    }

    // Ensure tag is added if it's not already added by core.
    if (empty($viewport_added)) {
      $attachments['#attached']['html_head'][] = [
        $viewport_tag,
        'viewport',
      ];
    }
  }
}