You are here

function hook_page_attachments in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Render/theme.api.php \hook_page_attachments()

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

Use this hook when you want to conditionally add attachments to a page.

If you want to alter the attachments added by other modules or if your module depends on the elements of other modules, use hook_page_attachments_alter() instead, which runs after this hook.

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

Parameters

array &$attachments: An array that you can add attachments to.

See also

hook_page_attachments_alter()

Related topics

1 string reference to 'hook_page_attachments'
PageRenderTest::testHookPageAttachmentsExceptions in core/modules/system/src/Tests/Common/PageRenderTest.php
Tests hook_page_attachments() exceptions.
7 functions implement hook_page_attachments()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

book_test_page_attachments in core/modules/book/tests/modules/book_test.module
Implements hook_page_attachments().
common_test_page_attachments in core/modules/system/tests/modules/common_test/common_test.module
Implements hook_page_attachments().
content_translation_page_attachments in core/modules/content_translation/content_translation.module
Implements hook_page_attachments().
contextual_page_attachments in core/modules/contextual/contextual.module
Implements hook_page_attachments().
quickedit_page_attachments in core/modules/quickedit/quickedit.module
Implements hook_page_attachments().

... See full list

1 invocation of hook_page_attachments()
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 978
Hooks and documentation related to the theme and render system.

Code

function hook_page_attachments(array &$attachments) {

  // Unconditionally attach an asset to the page.
  $attachments['#attached']['library'][] = 'core/domready';

  // Conditionally attach an asset to the page.
  if (!\Drupal::currentUser()
    ->hasPermission('may pet kittens')) {
    $attachments['#attached']['library'][] = 'core/jquery';
  }
}