Is it possible to alter html_head using hook_page_attachments_alter() per node? Is it possible to alter html_head using hook_page_attachments_alter() per node? I need to attach different &lt;script&gt; tags to a list of nodes. I am able to add it to each node, but all gets overridden with the last alteration.
$nodes = \Drupal::entityTypeManager()-&gt;getStorage('node')-&gt;loadByProperties(['type' =&gt; 'faq_page', 'status' =&gt; 1]);

foreach($nodes as $node) {
  $description = [
    [
      '#tag' =&gt; 'script',
    ],
    // …
  $page['#attached']['html_head'][] = [$description, 'description];
}
Yes, when you get the current node.
/**
 * Implements hook_page_attachments_alter().
 */
function MYMODULE_page_attachments_alter(array &amp;$attachments) {

  $node = \Drupal::routeMatch()-&gt;getParameter('node');

  if ($node instanceof \Drupal\node\NodeInterface) {

    $attachments['#attached']['html_head'][] = [
      [
        '#tag' =&gt; 'script',
        '#value' =&gt; 'alert(&quot;' . $node-&gt;getTitle() . '&quot;)',
      ],
      'MYMODULE_foobar',
    ];
  }
}


If this really should be in a foreach, you need to ensure to give each tag an unique key.
foreach ($nodes as $node) {
  $description = [
    [
      '#tag' =&gt; 'script',
    ],
    ...
  ];
  $page['#attached']['html_head'][] = [$description, 'description_' . $node-&gt;id()];
}