summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-04-26 17:59:28 (GMT)
committerAlex Pott2015-04-26 17:59:28 (GMT)
commit5b560a5eb00c9c71de469ac2cdd36e32b4b8365e (patch)
tree017e8dabaea36e38a0e388a94f085912d150cc54
parent0df58b068f3b6fa415d5ffd60014024749002bd1 (diff)
Issue #2475749 by mbovan: Allow to set #cache metadata in hook_page_attachments()
-rw-r--r--core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php8
-rw-r--r--core/modules/system/src/Tests/Common/PageRenderTest.php8
-rw-r--r--core/modules/system/tests/modules/common_test/common_test.module4
3 files changed, 14 insertions, 6 deletions
diff --git a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
index 17568d1..182f5a9 100644
--- a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
+++ b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
@@ -278,15 +278,15 @@ class HtmlRenderer implements MainContentRendererInterface {
$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.
diff --git a/core/modules/system/src/Tests/Common/PageRenderTest.php b/core/modules/system/src/Tests/Common/PageRenderTest.php
index b879f37..332239c 100644
--- a/core/modules/system/src/Tests/Common/PageRenderTest.php
+++ b/core/modules/system/src/Tests/Common/PageRenderTest.php
@@ -54,6 +54,10 @@ class PageRenderTest extends KernelTestBase {
$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 @@ class PageRenderTest extends KernelTestBase {
}
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 @@ class PageRenderTest extends KernelTestBase {
}
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);
}
diff --git a/core/modules/system/tests/modules/common_test/common_test.module b/core/modules/system/tests/modules/common_test/common_test.module
index 2323cdd..a6f72a7 100644
--- a/core/modules/system/tests/modules/common_test/common_test.module
+++ b/core/modules/system/tests/modules/common_test/common_test.module
@@ -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';