From 7e506626a260eb93eaebbc81b5d8e3a858dc5c6b Mon Sep 17 00:00:00 2001
From: xjm <xjm@65776.no-reply.drupal.org>
Date: Wed, 22 Feb 2017 23:10:25 +0000
Subject: [PATCH] Issue #2853592 by Lendude, dawehner: Cacheability metadata
 can't be set from within argument default handlers

---
 .../views/display/DisplayPluginBase.php       |  1 +
 .../argument_default/ArgumentDefaultTest.php  |  1 +
 .../tests/src/Kernel/Plugin/CacheTest.php     | 26 +++++++++++++++++++
 3 files changed, 28 insertions(+)

diff --git a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php
index 1a0741d28340..c8b07ea1e08b 100644
--- a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php
+++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php
@@ -2112,6 +2112,7 @@ public function render() {
       // Assigned by reference so anything added in $element['#attached'] will
       // be available on the view.
       '#attached' => &$this->view->element['#attached'],
+      '#cache' => &$this->view->element['#cache'],
     );
 
     $this->applyDisplayCacheabilityMetadata($this->view->element);
diff --git a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/argument_default/ArgumentDefaultTest.php b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/argument_default/ArgumentDefaultTest.php
index 6f35ba48b713..b2ad514f36ee 100644
--- a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/argument_default/ArgumentDefaultTest.php
+++ b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/argument_default/ArgumentDefaultTest.php
@@ -28,6 +28,7 @@ protected function defineOptions() {
    * {@inheritdoc}
    */
   public function getArgument() {
+    $this->view->element['#cache']['tags'][] = 'example_tag';
     return $this->options['value'];
   }
 
diff --git a/core/modules/views/tests/src/Kernel/Plugin/CacheTest.php b/core/modules/views/tests/src/Kernel/Plugin/CacheTest.php
index 31805d8d87c6..e1328d767e08 100644
--- a/core/modules/views/tests/src/Kernel/Plugin/CacheTest.php
+++ b/core/modules/views/tests/src/Kernel/Plugin/CacheTest.php
@@ -383,4 +383,30 @@ public function testCacheContextIntegration() {
     $this->assertIdenticalResultset($view, [['name' => 'Paul']], $map);
   }
 
+  /**
+   * Tests that cacheability metadata is carried over from argument defaults.
+   */
+  public function testArgumentDefaultCache() {
+    $view = Views::getView('test_view');
+
+    // Add a new argument and set the test plugin for the argument_default.
+    $options = array(
+      'default_argument_type' => 'argument_default_test',
+      'default_argument_options' => array(
+        'value' => 'John'
+      ),
+      'default_action' => 'default'
+    );
+    $view->addHandler('default', 'argument', 'views_test_data', 'name', $options);
+    $view->initHandlers();
+
+    $output = $view->preview();
+
+    /** @var \Drupal\Core\Render\RendererInterface $renderer */
+    $renderer = \Drupal::service('renderer');
+
+    $renderer->renderPlain($output);
+    $this->assertEquals(['config:views.view.test_view', 'example_tag'], $output['#cache']['tags']);
+  }
+
 }
-- 
GitLab