function common_test_page_attachments_alter

Same name and namespace in other branches
  1. 9 core/modules/system/tests/modules/common_test/common_test.module \common_test_page_attachments_alter()
  2. 8.9.x core/modules/system/tests/modules/common_test/common_test.module \common_test_page_attachments_alter()
  3. 10 core/modules/system/tests/modules/common_test/common_test.module \common_test_page_attachments_alter()

Implements hook_page_attachments_alter().

See also

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

File

core/modules/system/tests/modules/common_test/common_test.module, line 226

Code

function common_test_page_attachments_alter(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.