{"id":57278,"date":"2023-05-26T22:16:08","date_gmt":"2023-05-26T16:46:08","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=57278"},"modified":"2023-05-30T09:31:03","modified_gmt":"2023-05-30T04:01:03","slug":"add-inline-javascript-code-in-drupal-9","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/add-inline-javascript-code-in-drupal-9\/","title":{"rendered":"Add inline Javascript code in Drupal 9"},"content":{"rendered":"<p>In our Drupal projects, we have requirements to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements. So in the blog, we will see how we can achieve this by both methods.<\/p>\n<p>Drupal provides hooks to do this in a proper way. To add the inline javascript code in the page header, we can use <a href=\"\/\/api.drupal.org\/api\/drupal\/core%21lib%21Drupal%21Core%21Render%21theme.api.php\/function\/hook_page_attachments\/8.2.x\u201d\">hook_page_attachments()<\/a>, and for the page bottom, we can use hook <a href=\"\/\/api.drupal.org\/api\/drupal\/core%21lib%21Drupal%21Core%21Render%21theme.api.php\/function\/hook_page_bottom\/8.2.x\u201d\">hook_page_bottom() <\/a><\/p>\n<p>Let\u2019s understand this with examples.<\/p>\n<p><strong>Method 1: Add inline Javascript code in page header<\/strong><\/p>\n<pre>\/**\r\n* Implements hook_page_attachments().\r\n*\/\r\nfunction\u00a0<strong>MYMODULE_page_attachments<\/strong>(array &amp;$attachments)\u00a0{\r\n\/\/ You can optionally perform a check to make sure you target a specific page.\r\nif\u00a0(\\Drupal::routeMatch()-&gt;getRouteName() ==\u00a0'some.route') {\r\n\/\/ Add our JS.\r\n$attachments['#attached']['html_head'][] = [\r\n[\r\n'#tag'\u00a0=&gt;\u00a0'script',\r\n'#attributes'\u00a0=&gt; [\r\n'type'\u00a0=&gt;\u00a0'text\/javascript',\r\n],\r\n'#value'\u00a0=&gt; \\Drupal\\Core\\Render\\Markup::create('console.log(\u201cThis\u201d is my javascript code);\u2019),\r\n],\u00a0\u00a0'key_for_this_snippet',\r\n];\r\n}\r\n}<\/pre>\n<p><strong>Method 2: Add inline Javascript code in page bottom<\/strong><\/p>\n<pre>\/**\r\n* Implements hook_page_bottom().\r\n*\/\r\nfunction <strong>MYMODULE_page_bottom<\/strong>(array &amp;$page_bottom) {\r\n$user = \\Drupal::currentUser();\r\n$page_bottom['pixelthis_sticky_banner'] = [\r\n'#markup' =&gt; \\Drupal\\Core\\Render\\Markup::create('console.log(\u201cThis\u201d is my javascript code);\u2019),\r\n'#cache' =&gt; [\r\n'contexts' =&gt; ['user'],\r\n'tags' =&gt; ['user:' . $user-&gt;id()],\r\n],\r\n];\r\n}<\/pre>\n<p>You can add this JS script for global or page specific as per your requirements. For that, add conditions before rendering the markup. Also, we can add the same by using route and controller.<\/p>\n<p>If you still have questions, comment and join the discussion. I will be adding different ways in the upcoming blogs. Stay tuned.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>In our Drupal projects, we have requirements to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements. So in the blog, we will see how we can achieve this by both methods. Drupal provides hooks to do this in a [&hellip;]<\/p>\n","protected":false},"author":1508,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":112},"categories":[3602],"tags":[4884,5212],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57278"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1508"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=57278"}],"version-history":[{"count":5,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57278\/revisions"}],"predecessor-version":[{"id":57354,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57278\/revisions\/57354"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=57278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=57278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=57278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}