diff --git a/core/modules/comment/src/Tests/CommentRssTest.php b/core/modules/comment/src/Tests/CommentRssTest.php index b146e65..e7072fa 100644 --- a/core/modules/comment/src/Tests/CommentRssTest.php +++ b/core/modules/comment/src/Tests/CommentRssTest.php @@ -43,7 +43,6 @@ protected function setUp() { ])->save(); } - /** * Tests comments as part of an RSS feed. */ diff --git a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php index 9f8cff4..edeac92 100644 --- a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php +++ b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php @@ -199,7 +199,7 @@ protected function addRequestWithFormat($format) { } /** - * Tests rest export with enabled views render caching. + * Tests REST export with views render caching enabled. */ public function testRestRenderCaching() { $this->drupalLogin($this->adminUser); @@ -217,7 +217,7 @@ public function testRestRenderCaching() { $original = DisplayPluginBase::buildBasicRenderable('test_serializer_display_entity', 'rest_export_1'); - // Ensure that there is no corresponding render cache entry yet. + // Ensure that there is no corresponding render cache item yet. $original['#cache'] += ['contexts' => []]; $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts'], $this->container->getParameter('renderer.config')['required_cache_contexts']); @@ -289,7 +289,6 @@ public function testRestRenderCaching() { $this->assertTrue($render_cache->get($original)); } - /** * Tests the response format configuration. */ @@ -491,7 +490,7 @@ public function testLivePreview() { } /** - * Tests the views interface for rest export displays. + * Tests the views interface for REST export displays. */ public function testSerializerViewsUI() { $this->drupalLogin($this->adminUser); diff --git a/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php b/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php index 500a00f..494a6b8 100644 --- a/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php +++ b/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php @@ -82,9 +82,9 @@ protected function assertPageCacheContextsAndTags(Url $url, array $expected_cont /** * Provides debug information for cache tags. * - * @param array $actual_tags + * @param string[] $actual_tags * The actual cache tags. - * @param array $expected_tags + * @param string[] $expected_tags * The expected cache tags. */ protected function debugCacheTags(array $actual_tags, array $expected_tags) { diff --git a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php index 8370b20..de57b07 100644 --- a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php @@ -12,6 +12,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; @@ -2140,25 +2141,23 @@ public function render() { } /** - * Setups the render cache information onto a view render element + * Sets up the render cache information onto a view render element * * @param array $element * The render element enhanced with cache tags, contexts and max age. */ protected function renderSetCacheData(array &$element) { - $element += ['#cache' => []]; - $element['#cache'] += ['tags' => [], 'contexts' => [], 'max-age' => CacheBackendInterface::CACHE_PERMANENT]; - - // If the output is a render array, add cache tags, regardless of whether - // caching is enabled or not; cache tags must always be set. - $element['#cache']['tags'] = Cache::mergeTags($element['#cache']['tags'], $this->view->getCacheTags()); - - $display_contexts = isset($this->display['cache_metadata']['contexts']) ? (array) $this->display['cache_metadata']['contexts'] : []; - $element['#cache']['contexts'] = Cache::mergeTags($element['#cache']['contexts'], $display_contexts); - /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */ $cache = $this->getPlugin('cache'); - $element['#cache']['max-age'] = Cache::mergeMaxAges($element['#cache']['max-age'], $cache->getCacheMaxAge()); + + $cacheability = new CacheableMetadata(); + $cacheability->setCacheTags($this->view->getCacheTags()); + $cacheability->setCacheContexts($this->display['cache_metadata']['contexts']); + $cacheability->setCacheMaxAge($cache->getCacheMaxAge()); + + $cacheability + ->merge(CacheableMetadata::createFromRenderArray($element)) + ->applyTo($element); } /** @@ -2335,6 +2334,7 @@ public function buildRenderable(array $args = [], $cache = TRUE) { '#arguments' => $args, '#embed' => FALSE, '#view' => $this->view, + '#cache_properties' => ['#view_id', '#view_display_show_admin_links', '#view_display_plugin_id'], ]; if ($cache) { @@ -2343,15 +2343,13 @@ public function buildRenderable(array $args = [], $cache = TRUE) { // additional cache context. $this->view->element['#cache']['keys'] = array_merge(['views', 'display', $this->view->element['#name'], $this->view->element['#display_id']], $this->view->element['#cache']['keys']); - $this->view->element['#cache_properties'] = ['#view_id', '#view_display_show_admin_links', '#view_display_plugin_id']; - $this->renderSetCacheData($this->view->element); } else { - unset($this->view->element['#cache']); + // Remove the cacheablity. + unset($this->view->element['#cache']['keys']); } - return $this->view->element; } diff --git a/core/modules/views/src/Plugin/views/display/DisplayPluginInterface.php b/core/modules/views/src/Plugin/views/display/DisplayPluginInterface.php index 3ee5409..9f9c547 100644 --- a/core/modules/views/src/Plugin/views/display/DisplayPluginInterface.php +++ b/core/modules/views/src/Plugin/views/display/DisplayPluginInterface.php @@ -474,8 +474,8 @@ public function execute(); * @param bool $cache * (optional) Specify FALSE in order to opt out of render caching. * - * @return array The render array of a view. - * The render array of a view. + * @return array + * The render array of a view. */ public function buildRenderable(array $args = [], $cache = TRUE); diff --git a/core/modules/views/src/Plugin/views/display/Feed.php b/core/modules/views/src/Plugin/views/display/Feed.php index 59b8d1a..9e5ca26 100644 --- a/core/modules/views/src/Plugin/views/display/Feed.php +++ b/core/modules/views/src/Plugin/views/display/Feed.php @@ -72,6 +72,8 @@ protected function getType() { public static function returnResponse($view_id, $display_id, array $args = []) { $build = static::buildBasicRenderable($view_id, $display_id, $args); + // Setup an empty response, so for example RSS can setup the proper + // Content-type header. $response = new CacheableResponse('', 200); $build['#response'] = $response; diff --git a/core/modules/views/src/Plugin/views/display/Page.php b/core/modules/views/src/Plugin/views/display/Page.php index 9ee09f6..e66177e 100644 --- a/core/modules/views/src/Plugin/views/display/Page.php +++ b/core/modules/views/src/Plugin/views/display/Page.php @@ -163,7 +163,6 @@ public static function buildBasicRenderable($view_id, $display_id, array $args = return $build; } - /** * Overrides \Drupal\views\Plugin\views\display\PathPluginBase::execute(). */ diff --git a/core/modules/views/src/Plugin/views/sort/Random.php b/core/modules/views/src/Plugin/views/sort/Random.php index 434a325..6f682e5 100644 --- a/core/modules/views/src/Plugin/views/sort/Random.php +++ b/core/modules/views/src/Plugin/views/sort/Random.php @@ -26,6 +26,7 @@ public function usesGroupBy() { public function query() { $this->query->addOrderBy('rand'); + // @todo Replace this once https://www.drupal.org/node/2464427 is in. $this->view->element['#cache']['max-age'] = 0; } diff --git a/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php b/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php index 0ac8141..eac7f0c 100644 --- a/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php +++ b/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php @@ -115,7 +115,7 @@ protected function assertViewsCacheTags(ViewExecutable $view, $expected_results_ * * @param \Drupal\views\ViewExecutable $view * The view. - * @param array $expected_render_array_cache_tags + * @param string[] $expected_render_array_cache_tags * The expected render cache tags. * @param bool $views_caching_is_enabled * Defines whether views output / render caching is enabled. diff --git a/core/modules/views/src/Tests/Handler/SortRandomTest.php b/core/modules/views/src/Tests/Handler/SortRandomTest.php index aafa0a9..f22a25e 100644 --- a/core/modules/views/src/Tests/Handler/SortRandomTest.php +++ b/core/modules/views/src/Tests/Handler/SortRandomTest.php @@ -31,8 +31,8 @@ class SortRandomTest extends ViewUnitTestBase { * * In total we have then 60 entries, which makes a probability of a collision * of 1/60!, which is around 1/1E80, which is higher than the estimated amount - * of protons / electrons in the observable universe, also called the eddington - * number. + * of protons / electrons in the observable universe, also called the + * eddington number. * * @see http://en.wikipedia.org/wiki/Eddington_number */ @@ -108,7 +108,7 @@ public function testRandomOrdering() { /** * Tests random ordering with tags based caching. * - * The random sorting should opt out of caching by defining a max age 0. + * The random sorting should opt out of caching by defining a max age of 0. */ public function testRandomOrderingWithRenderCaching() { $view_random = $this->getBasicRandomView(); diff --git a/core/modules/views/src/Tests/RenderCacheIntegrationTest.php b/core/modules/views/src/Tests/RenderCacheIntegrationTest.php index ce61025..f1e870d 100644 --- a/core/modules/views/src/Tests/RenderCacheIntegrationTest.php +++ b/core/modules/views/src/Tests/RenderCacheIntegrationTest.php @@ -84,7 +84,7 @@ public function testFieldBasedViewCacheTagsWithCachePluginTime() { * * @param bool $do_assert_views_caches * Whether to check Views' result & output caches. - */ + */ protected function assertCacheTagsForFieldBasedView($do_assert_views_caches) { $this->pass('Checking cache tags for field-based view.'); $view = Views::getview('entity_test_fields'); diff --git a/core/modules/views/src/Views.php b/core/modules/views/src/Views.php index 196d8e4..747f2f8 100644 --- a/core/modules/views/src/Views.php +++ b/core/modules/views/src/Views.php @@ -8,7 +8,6 @@ namespace Drupal\views; use Drupal\Component\Utility\SafeMarkup; -use Drupal\views\Plugin\views\display\Page; /** * Static service container wrapper for views. diff --git a/core/modules/views/views.theme.inc b/core/modules/views/views.theme.inc index 9aa4320..98604a0 100644 --- a/core/modules/views/views.theme.inc +++ b/core/modules/views/views.theme.inc @@ -920,8 +920,8 @@ function template_preprocess_views_view_row_rss(&$variables) { /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = \Drupal::service('renderer'); - // We render the item description it might contain entities, which attach - // rss elements via hook_entity_view. + // We render the item description. It might contain entities, which attach rss + // elements via hook_entity_view, see comment_entity_view(). $variables['description'] = is_array($item->description) ? $renderer->render($item->description) : $item->description; $variables['item_elements'] = array(); foreach ($item->elements as $element) {