function d8_google_optimize_hide_page_page_attachments in Drupal 8 Google Optimize Hide Page 8
Implements hook_page_attachments().
Attach JavaScript to the appropriate scope/region of the page.
File
Code
function d8_google_optimize_hide_page_page_attachments(array &$attachments) {
if (!d8_google_optimize_hide_page_active()) {
return;
}
$class_name = d8_google_optimize_hide_page_class_name();
$isExternalFile = d8_google_optimize_hide_page_external_file();
if ($isExternalFile) {
/** @var \Drupal\Core\Routing\UrlGeneratorInterface $urlGenerator */
$urlGenerator = \Drupal::service('url_generator');
$cssPath = drupal_get_path('module', 'd8_google_optimize_hide_page') . '/d8_google_optimize_hide_page.css';
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => [
'rel' => 'stylesheet',
'href' => $cssPath,
],
'#weight' => -16,
],
'd8-google-optimize-hide-page-css',
];
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#attributes' => [
'src' => $urlGenerator
->generateFromRoute('d8_google_optimize_hide_page.snippet'),
],
'#weight' => -15,
],
"d8-google-optimize-hide-page-js",
];
}
else {
$snippet = new SnippetGenerator();
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => $snippet
->getSnippet(),
'#attributes' => [],
],
'd8-google-optimize-hide-page-js',
];
$css = '.' . $class_name . ' { opacity: 0 !important} ';
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'style',
'#value' => $css,
],
'd8-google-optimize-hide-page-css',
];
}
}