function hook_page_attachments_alter

Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Render/theme.api.php \hook_page_attachments_alter()
  2. 10 core/lib/Drupal/Core/Render/theme.api.php \hook_page_attachments_alter()
  3. 11.x core/lib/Drupal/Core/Render/theme.api.php \hook_page_attachments_alter()

Alter attachments (typically assets) to a page before it is rendered.

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 (this hook runs after hook_page_attachments(). This hook can be implemented by both modules and themes.

If you try to add anything but #attached and #cache to the array, an exception is thrown.

Parameters

array &$attachments: Array of all attachments provided by hook_page_attachments() implementations.

See also

hook_page_attachments()

Related topics

1 string reference to 'hook_page_attachments_alter'
PageRenderTest::testHookPageAlter in core/modules/system/tests/src/Kernel/Common/PageRenderTest.php
Tests hook_page_attachments_alter() exceptions.
6 functions implement hook_page_attachments_alter()

Note: the procedural functions in this list are found by pattern matching, so the list may include some functions that are not actually implementations of this hook.

CommonTestThemeHooks::pageAttachmentsAlter in core/modules/system/tests/modules/common_test/src/Hook/CommonTestThemeHooks.php
Implements hook_page_attachments_alter().
common_test_page_attachments_alter in core/modules/system/tests/modules/common_test/common_test.module
Implements hook_page_attachments_alter().
ContextualTestHooks::pageAttachmentsAlter in core/modules/contextual/tests/modules/contextual_test/src/Hook/ContextualTestHooks.php
Implements hook_page_attachments_alter().
contextual_test_page_attachments_alter in core/modules/contextual/tests/modules/contextual_test/contextual_test.module
Implements hook_page_attachments_alter().
SystemModuleTestHooks::pageAttachmentsAlter in core/modules/system/tests/modules/system_module_test/src/Hook/SystemModuleTestHooks.php
Implements hook_page_attachments_alter().

... See full list

1 invocation of hook_page_attachments_alter()
HtmlRenderer::invokePageAttachmentHooks in core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
Invokes the page attachment hooks.

File

core/lib/Drupal/Core/Render/theme.api.php, line 1074

Code

function hook_page_attachments_alter(array &$attachments) {
    // Conditionally remove an asset.
    if (in_array('core/jquery', $attachments['#attached']['library'])) {
        $index = array_search('core/jquery', $attachments['#attached']['library']);
        unset($attachments['#attached']['library'][$index]);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.