// to add library to all pages
/**
* Implements hook_page_attachments_alter().
*/
function hook_page_attachments_alter(&$attachments) {
$attachments['#attached']['library'][] = 'module_name/library_name';
// Remove the stock Drupal favicon if it is present.
if (isset($attachments['#attached']['html_head_link'])) {
foreach ($attachments['#attached']['html_head_link'] as $key => $config) {
if (isset($config[0]['rel']) && $config[0]['rel'] === 'shortcut icon') {
unset($attachments['#attached']['html_head_link'][$key]);
}
}
}
}
/**
* Implements hook_page_attachments().
*/
function MODULE_NAME_page_attachments(array &$attachments) {
// You can optionally perform a check to make sure you target a specific page.
if (\Drupal::routeMatch()->getRouteName() == 'some.route') {
// Add our JS.
$attachments['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => [
'type' => 'text/javascript',
],
'#value' => 'console.log("i am here");',
],
'key_for_this_snippet',
];
}
}