hook_page_attachments_alter does not allow altering all of a page's attachments

Created on 27 October 2020, about 4 years ago
Updated 4 February 2023, almost 2 years ago

Problem/Motivation

The documentation of hook_page_attachments_alter() states:

Use this hook when you want to remove or alter attachments on the page, or add attachments to the page that depend on another module's attachments [...].

However, this hook only allows you to modify attachments that were added using hook_page_attachments(). Attachments added through a render array's #attached-Property can not be altered this way. One example would be the link relationships that NodeViewController::view() adds. If you need to alter or remove those, your main option is to replace the controller for affected routes with your own. This becomes a lot of work if multiple controllers are involved in the change you need to make.

Proposed resolution

A couple of solutions come to mind:

  1. Invoke hoook_page_attachments_alter() much later, so all attachments have already been collected. This is the case after the actual render step in HtmlRenderer::renderResponse(). But we might have to collect bubbleable metadata again afterwards, or even run rendering again - I'm not entirely sure.
  2. Add a new hook that runs later in HtmlRenderer::renderResponse(), so all page attachments have been collected.

Remaining tasks

Decide the correct approach to take.

API changes

Possibly add a new hook, or change the time at which hook_page_attachments_alter() is called.

Release notes snippet

-

๐Ÿ› Bug report
Status

Needs work

Version

10.1 โœจ

Component
Themeย  โ†’

Last updated 4 days ago

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany Mirroar

Live updates comments and jobs are added and updated live.
  • Needs subsystem maintainer review

    It is used to alert the maintainer(s) of a particular core subsystem that an issue significantly impacts their subsystem, and their signoff is needed (see the governance policy draft for more information). Also, if you use this tag, make sure the issue component is set to the correct subsystem. If an issue significantly impacts more than one subsystem, use needs framework manager review instead.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024