Commit 5b560a5e authored by alexpott's avatar alexpott

Issue #2475749 by mbovan: Allow to set #cache metadata in hook_page_attachments()

parent 0df58b06
......@@ -278,15 +278,15 @@ public function invokePageAttachmentHooks(array &$page) {
$function = $module . '_page_attachments';
$function($attachments);
}
if (array_diff(array_keys($attachments), ['#attached', '#post_render_cache']) !== []) {
throw new \LogicException('Only #attached and #post_render_cache may be set in hook_page_attachments().');
if (array_diff(array_keys($attachments), ['#attached', '#post_render_cache', '#cache']) !== []) {
throw new \LogicException('Only #attached, #post_render_cache and #cache may be set in hook_page_attachments().');
}
// Modules and themes can alter page attachments.
$this->moduleHandler->alter('page_attachments', $attachments);
\Drupal::theme()->alter('page_attachments', $attachments);
if (array_diff(array_keys($attachments), ['#attached', '#post_render_cache']) !== []) {
throw new \LogicException('Only #attached and #post_render_cache may be set in hook_page_attachments_alter().');
if (array_diff(array_keys($attachments), ['#attached', '#post_render_cache', '#cache']) !== []) {
throw new \LogicException('Only #attached, #post_render_cache and #cache may be set in hook_page_attachments_alter().');
}
// Merge the attachments onto the $page render array.
......
......@@ -54,6 +54,10 @@ function assertPageRenderHookExceptions($module, $hook) {
$page = [];
$html_renderer->invokePageAttachmentHooks($page);
// Assert that hooks can set cache tags.
$this->assertEqual($page['#cache']['tags'], ['example']);
$this->assertEqual($page['#cache']['contexts'], ['user.permissions']);
// Assert an invalid hook implementation doesn't trigger an exception.
\Drupal::state()->set($module . '.' . $hook . '.descendant_attached', TRUE);
$assertion = $hook . '() implementation that sets #attached on a descendant triggers an exception';
......@@ -64,7 +68,7 @@ function assertPageRenderHookExceptions($module, $hook) {
}
catch (\LogicException $e) {
$this->pass($assertion);
$this->assertEqual($e->getMessage(), 'Only #attached and #post_render_cache may be set in ' . $hook . '().');
$this->assertEqual($e->getMessage(), 'Only #attached, #post_render_cache and #cache may be set in ' . $hook . '().');
}
\Drupal::state()->set('bc_test.' . $hook . '.descendant_attached', FALSE);
......@@ -78,7 +82,7 @@ function assertPageRenderHookExceptions($module, $hook) {
}
catch (\LogicException $e) {
$this->pass($assertion);
$this->assertEqual($e->getMessage(), 'Only #attached and #post_render_cache may be set in ' . $hook . '().');
$this->assertEqual($e->getMessage(), 'Only #attached, #post_render_cache and #cache may be set in ' . $hook . '().');
}
\Drupal::state()->set($module . '.' . $hook . '.render_array', FALSE);
}
......
......@@ -224,6 +224,8 @@ function common_test_cron() {
function common_test_page_attachments(array &$page) {
$page['#attached']['library'][] = 'core/foo';
$page['#attached']['library'][] = 'core/bar';
$page['#cache']['tags'] = ['example'];
$page['#cache']['contexts'] = ['user.permissions'];
if (\Drupal::state()->get('common_test.hook_page_attachments.descendant_attached', FALSE)) {
$page['content']['#attached']['library'][] = 'core/jquery';
......@@ -248,6 +250,8 @@ function common_test_page_attachments_alter(array &$page) {
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';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment