Skip to content

Instantly share code, notes, and snippets.

@robdecker robdecker/1.php
Created Feb 20, 2020

Embed
What would you like to do?
[Add inline JavaScript to a page] #d8 #js
/**
* 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");',
],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.