Which build/alter hook recommended to use to add to pages?

Created on 4 June 2024, 6 months ago
Updated 14 August 2024, 3 months ago

Problem/Motivation

This is great as an addition to the usual way of requiring assets with any render array, but for replacing hook_page_attachments(), what is recommended?

Hmm it seems hook_page_top() should have worked?

$page_top['#attached']['js'][] = [ … ] or should that have been $page_top['#attached'][]['js'] = [ … ]

(Needed to do several in a row, sorry bailed on this approach before completing this issue and realizing an easily testable error, but perhaps documentation of attaching multiple separate sets of inline JavaScript would help, separate so that conditional logic can decide which are loaded.)

πŸ’¬ Support request
Status

Fixed

Version

1.0

Component

Documentation

Created by

πŸ‡ΊπŸ‡ΈUnited States mlncn Minneapolis, MN, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @mlncn
  • πŸ‡¨πŸ‡¦Canada gapple

    Anywhere with an #attached, just like with libraries, so hook_page_attachments() is fine. The attached scripts are added to the page with library scripts - in the header or before the end of the body as configured on each item's scope attribute.

    Though as always, I will recommend using a proper library with a JavaScript file, and passing data via drupalSettings, rather than using inline scripts.

  • Status changed to Fixed 4 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024