function CommonTestHooks::pageAttachmentsAlter

Implements hook_page_attachments_alter().

See also

\Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()

File

core/modules/system/tests/modules/common_test/src/Hook/CommonTestHooks.php, line 150

Class

CommonTestHooks
Hook implementations for common_test.

Namespace

Drupal\common_test\Hook

Code

public function pageAttachmentsAlter(array &$page) {
    // Remove a library that was added in common_test_page_attachments(), to test
    // that this hook can do what it claims to do.
    if (isset($page['#attached']['library']) && ($index = array_search('core/bar', $page['#attached']['library'])) && $index !== FALSE) {
        unset($page['#attached']['library'][$index]);
    }
    $page['#attached']['library'][] = 'core/baz';
    $page['#cache']['tags'] = [
        'example',
    ];
    $page['#cache']['contexts'] = [
        'user.permissions',
    ];
    if (\Drupal::state()->get('common_test.hook_page_attachments_alter.descendant_attached', FALSE)) {
        $page['content']['#attached']['library'][] = 'core/jquery';
    }
    if (\Drupal::state()->get('common_test.hook_page_attachments_alter.render_array', FALSE)) {
        $page['something'] = [
            '#markup' => 'test',
        ];
    }
}

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