summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2014-03-18 10:17:33 +0000
committerNathaniel Catchpole2014-03-18 10:17:33 +0000
commit2ca54697b3637a1df60639d092c9f708dabf2aa3 (patch)
tree24e293e17fbce86642f1a2bc0f397333feef5afe
parent2cfe18943b574c8c0be0a36b6bfd74994333d9c3 (diff)
Issue #2215719 by Wim Leers: Cache tags set by #pre_render callbacks are lost in page cache.8.0-alpha10
-rw-r--r--core/includes/common.inc14
-rw-r--r--core/includes/theme.inc8
-rw-r--r--core/lib/Drupal/Core/Controller/HtmlControllerBase.php15
-rw-r--r--core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php7
-rw-r--r--core/lib/Drupal/Core/Page/HtmlPage.php10
-rw-r--r--core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php2
-rw-r--r--core/modules/system/lib/Drupal/system/Tests/Common/RenderTest.php8
-rw-r--r--core/modules/system/tests/modules/ajax_test/lib/Drupal/ajax_test/Form/AjaxTestForm.php1
-rw-r--r--core/modules/system/tests/modules/system_test/lib/Drupal/system_test/Controller/SystemTestController.php17
9 files changed, 52 insertions, 30 deletions
diff --git a/core/includes/common.inc b/core/includes/common.inc
index 6d91783..a2226c8 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -3990,6 +3990,12 @@ function drupal_render(&$elements, $is_recursive_call = FALSE) {
$suffix = isset($elements['#suffix']) ? $elements['#suffix'] : '';
$elements['#markup'] = $prefix . $elements['#children'] . $suffix;
+ // Collect all cache tags. This allows the caller of drupal_render() to also
+ // access the complete list of cache tags.
+ if (!$is_recursive_call || isset($elements['#cache'])) {
+ $elements['#cache']['tags'] = drupal_render_collect_cache_tags($elements);
+ }
+
// Cache the processed element if #cache is set.
if (isset($elements['#cache'])) {
drupal_render_cache_set($elements['#markup'], $elements);
@@ -4196,10 +4202,14 @@ function drupal_render_cache_set(&$markup, array $elements) {
$data['#post_render_cache'] = $elements['#post_render_cache'];
}
+ // Persist cache tags associated with this element.
+ if (isset($elements['#cache']['tags'])) {
+ $data['#cache']['tags'] = $elements['#cache']['tags'];
+ }
+
$bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';
$expire = isset($elements['#cache']['expire']) ? $elements['#cache']['expire'] : Cache::PERMANENT;
- $tags = drupal_render_collect_cache_tags($elements);
- \Drupal::cache($bin)->set($cid, $data, $expire, $tags);
+ \Drupal::cache($bin)->set($cid, $data, $expire, $elements['#cache']['tags']);
}
/**
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index cfd8427..f699d4b 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -1967,14 +1967,6 @@ function template_preprocess_html(&$variables) {
}
}
- // Initializes attributes which are specific to the html and body elements.
- $variables['html_attributes'] = new Attribute;
-
- // HTML element attributes.
- $language_interface = \Drupal::service('language_manager')->getCurrentLanguage();
- $variables['html_attributes']['lang'] = $language_interface->id;
- $variables['html_attributes']['dir'] = $language_interface->direction ? 'rtl' : 'ltr';
-
$site_config = \Drupal::config('system.site');
// Construct page title.
if ($page->hasTitle()) {
diff --git a/core/lib/Drupal/Core/Controller/HtmlControllerBase.php b/core/lib/Drupal/Core/Controller/HtmlControllerBase.php
index 9cc7651..6b81e7e 100644
--- a/core/lib/Drupal/Core/Controller/HtmlControllerBase.php
+++ b/core/lib/Drupal/Core/Controller/HtmlControllerBase.php
@@ -71,9 +71,9 @@ class HtmlControllerBase {
);
}
- $cache_tags = $this->drupalRenderCollectCacheTags($page_content);
- $cache = !empty($cache_tags) ? array('tags' => $cache_tags) : array();
- $fragment = new HtmlFragment($this->drupalRender($page_content), $cache);
+ $content = $this->drupalRender($page_content);
+ $cache = !empty($page_content['#cache']['tags']) ? array('tags' => $page_content['#cache']['tags']) : array();
+ $fragment = new HtmlFragment($content, $cache);
// A title defined in the return always wins.
if (isset($page_content['#title'])) {
@@ -95,13 +95,4 @@ class HtmlControllerBase {
return drupal_render($elements, $is_recursive_call);
}
- /**
- * Wraps drupal_render_collect_cache_tags()
- *
- * @todo: Remove as part of https://drupal.org/node/2182149
- */
- protected function drupalRenderCollectCacheTags($element, $tags = array()) {
- return drupal_render_collect_cache_tags($element, $tags);
- }
-
}
diff --git a/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php b/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php
index 6e0c509..e1a9029 100644
--- a/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php
+++ b/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php
@@ -48,15 +48,14 @@ class DefaultHtmlFragmentRenderer implements HtmlFragmentRendererInterface {
// hook_page_build(). This adds the other regions to the page.
$page_array = drupal_prepare_page($page_content);
- // Collect cache tags for all the content in all the regions on the page.
- $tags = drupal_render_collect_cache_tags($page_array);
-
// Build the HtmlPage object.
- $page = new HtmlPage('', array('tags' => $tags), $fragment->getTitle());
+ $page = new HtmlPage('', array(), $fragment->getTitle());
$page = $this->preparePage($page, $page_array);
$page->setBodyTop(drupal_render($page_array['page_top']));
$page->setBodyBottom(drupal_render($page_array['page_bottom']));
$page->setContent(drupal_render($page_array));
+ // Collect cache tags for all the content in all the regions on the page.
+ $page->setCacheTags($page_array['#cache']['tags']);
$page->setStatusCode($status_code);
return $page;
diff --git a/core/lib/Drupal/Core/Page/HtmlPage.php b/core/lib/Drupal/Core/Page/HtmlPage.php
index c2f09ed..f8725c4 100644
--- a/core/lib/Drupal/Core/Page/HtmlPage.php
+++ b/core/lib/Drupal/Core/Page/HtmlPage.php
@@ -158,5 +158,15 @@ class HtmlPage extends HtmlFragment {
return $this->statusCode;
}
+ /**
+ * Sets the cache tags associated with this HTML page.
+ *
+ * @param array $cache_tags
+ * The cache tags associated with this HTML page.
+ */
+ public function setCacheTags(array $cache_tags) {
+ $this->cache['tags'] = $cache_tags;
+ }
+
}
diff --git a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php
index 3ca1f19..7f961d3 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php
@@ -65,7 +65,7 @@ class PageCacheTest extends WebTestBase {
$cid_parts = array(url($path, array('absolute' => TRUE)), 'html');
$cid = sha1(implode(':', $cid_parts));
$cache_entry = \Drupal::cache('page')->get($cid);
- $this->assertIdentical($cache_entry->tags, array('content:1', 'system_test_cache_tags_page:1'));
+ $this->assertIdentical($cache_entry->tags, array('content:1', 'system_test_cache_tags_page:1', 'pre_render:1'));
Cache::invalidateTags($tags);
$this->drupalGet($path);
diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/RenderTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/RenderTest.php
index b2d7eb0..14ce926 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Common/RenderTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Common/RenderTest.php
@@ -500,6 +500,7 @@ class RenderTest extends DrupalUnitTestBase {
'#markup' => '<p>#cache enabled, GET</p>',
'#attached' => $test_element['#attached'],
'#post_render_cache' => $test_element['#post_render_cache'],
+ '#cache' => array('tags' => array()),
);
$this->assertIdentical($cached_element, $expected_element, 'The correct data is cached: the stored #markup and #attached properties are not affected by #post_render_cache callbacks.');
@@ -619,6 +620,7 @@ class RenderTest extends DrupalUnitTestBase {
$context_3,
)
),
+ '#cache' => array('tags' => array()),
);
$dom = Html::load($cached_element['#markup']);
@@ -702,6 +704,7 @@ class RenderTest extends DrupalUnitTestBase {
$context_3,
)
),
+ '#cache' => array('tags' => array()),
);
$dom = Html::load($cached_parent_element['#markup']);
@@ -727,6 +730,7 @@ class RenderTest extends DrupalUnitTestBase {
$context_3,
)
),
+ '#cache' => array('tags' => array()),
);
$dom = Html::load($cached_child_element['#markup']);
@@ -830,6 +834,7 @@ class RenderTest extends DrupalUnitTestBase {
$expected_token => $context,
),
),
+ '#cache' => array('tags' => array()),
);
$this->assertIdentical($cached_element, $expected_element, 'The correct data is cached: the stored #markup and #attached properties are not affected by #post_render_cache callbacks.');
@@ -920,6 +925,7 @@ class RenderTest extends DrupalUnitTestBase {
$expected_token => $context,
),
),
+ '#cache' => array('tags' => array()),
);
$this->assertIdentical($cached_element, $expected_element, 'The correct data is cached for the child element: the stored #markup and #attached properties are not affected by #post_render_cache callbacks.');
@@ -944,6 +950,7 @@ class RenderTest extends DrupalUnitTestBase {
$expected_token => $context,
),
),
+ '#cache' => array('tags' => array()),
);
$this->assertIdentical($cached_element, $expected_element, 'The correct data is cached for the parent element: the stored #markup and #attached properties are not affected by #post_render_cache callbacks.');
@@ -972,6 +979,7 @@ class RenderTest extends DrupalUnitTestBase {
$expected_token => $context,
),
),
+ '#cache' => array('tags' => array()),
);
$this->assertIdentical($cached_element, $expected_element, 'The correct data is cached for the child element: the stored #markup and #attached properties are not affected by #post_render_cache callbacks.');
diff --git a/core/modules/system/tests/modules/ajax_test/lib/Drupal/ajax_test/Form/AjaxTestForm.php b/core/modules/system/tests/modules/ajax_test/lib/Drupal/ajax_test/Form/AjaxTestForm.php
index 980d117..462f671 100644
--- a/core/modules/system/tests/modules/ajax_test/lib/Drupal/ajax_test/Form/AjaxTestForm.php
+++ b/core/modules/system/tests/modules/ajax_test/lib/Drupal/ajax_test/Form/AjaxTestForm.php
@@ -27,7 +27,6 @@ class AjaxTestForm implements FormInterface {
public function buildForm(array $form, array &$form_state) {
$form['#action'] = url('ajax-test/dialog');
- $form['#cache'] = TRUE;
$form['description'] = array(
'#markup' => '<p>' . t("Ajax Form contents description.") . '</p>',
diff --git a/core/modules/system/tests/modules/system_test/lib/Drupal/system_test/Controller/SystemTestController.php b/core/modules/system/tests/modules/system_test/lib/Drupal/system_test/Controller/SystemTestController.php
index 868117a..36a2c8f 100644
--- a/core/modules/system/tests/modules/system_test/lib/Drupal/system_test/Controller/SystemTestController.php
+++ b/core/modules/system/tests/modules/system_test/lib/Drupal/system_test/Controller/SystemTestController.php
@@ -41,15 +41,28 @@ class SystemTestController extends ControllerBase {
/**
* Set cache tag on on the returned render array.
*/
- function system_test_cache_tags_page() {
+ public function system_test_cache_tags_page() {
$build['main'] = array(
- '#markup' => 'Cache tags page example',
'#cache' => array('tags' => array('system_test_cache_tags_page' => TRUE)),
+ '#pre_render' => array(
+ '\Drupal\system_test\Controller\SystemTestController::preRenderCacheTags',
+ ),
+ 'message' => array(
+ '#markup' => 'Cache tags page example',
+ ),
);
return $build;
}
/**
+ * Sets a cache tag on an element to help test #pre_render and cache tags.
+ */
+ public static function preRenderCacheTags($elements) {
+ $elements['#cache']['tags']['pre_render'] = TRUE;
+ return $elements;
+ }
+
+ /**
* @todo Remove system_test_authorize_init_page().
*/
public function authorizeInit($page_title) {