Commit f5d384cb authored by catch's avatar catch

Issue #2378815 by damiankloip, dawehner: DisplayPluginBase::elementPreRender()...

Issue #2378815 by damiankloip, dawehner: DisplayPluginBase::elementPreRender() and View::preRenderViewElement() are called even when the Views output cache is being used, causing notice
parent fe04699e
......@@ -24,10 +24,10 @@ views.cache.time:
type: mapping
label: 'Cache options'
mapping:
result_lifespan:
results_lifespan:
type: integer
label: 'The length of time raw query results should be cached.'
result_lifespan_custom:
results_lifespan_custom:
type: integer
label: 'Length of time in seconds raw query results should be cached.'
output_lifespan:
......
......@@ -60,7 +60,16 @@ public static function preRenderViewElement($element) {
// $title_suffix variable (which we will then render in
// views-view.html.twig).
$view->setDisplay($element['#display_id']);
$element += $view->executeDisplay($element['#display_id'], $element['#arguments']);
// Add the result of the executed view as a child element so any
// #pre_render elements for the view will get processed. A #pre_render
// element cannot be added to the main element as this is already inside
// a #pre_render callback.
$element['view_build'] = $view->executeDisplay($element['#display_id'], $element['#arguments']);
if (isset($element['view_build']['#title'])) {
$element['#title'] = &$element['view_build']['#title'];
}
views_add_contextual_links($element, 'view', $view, $view->current_display);
}
}
......
......@@ -74,7 +74,7 @@ abstract class CachePluginBase extends PluginBase {
protected $outputKey;
/**
* The renderer service.
* The HTML renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
......@@ -90,7 +90,7 @@ abstract class CachePluginBase extends PluginBase {
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* The HTML renderer.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
......@@ -181,8 +181,16 @@ public function cacheSet($type) {
\Drupal::cache($this->resultsBin)->set($this->generateResultsKey(), $data, $this->cacheSetExpire($type), $this->getCacheTags());
break;
case 'output':
$this->renderer->render($this->view->display_handler->output);
$this->storage = $this->renderer->getCacheableRenderArray($this->view->display_handler->output);
// Make a copy of the output so it is not modified. If we render the
// display output directly an empty string will be returned when the
// view is actually rendered. If we try to set '#printed' to FALSE there
// are problems with asset bubbling.
$output = $this->view->display_handler->output;
$this->renderer->render($output);
// Also assign the cacheable render array back to the display handler so
// that is used to render the view for this request and rendering does
// not happen twice.
$this->storage = $this->view->display_handler->output = $this->renderer->getCacheableRenderArray($output);
\Drupal::cache($this->outputBin)->set($this->generateOutputKey(), $this->storage, $this->cacheSetExpire($type), $this->getCacheTags());
break;
}
......
......@@ -48,7 +48,7 @@ class Time extends CachePluginBase {
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* The HTML renderer.
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter
* The date formatter service.
*/
......
......@@ -2280,7 +2280,6 @@ public function buildRenderable(array $args = []) {
'#display_id' => $this->display['id'],
'#arguments' => $args,
'#embed' => FALSE,
'#pre_render' => [['\Drupal\views\Element\View', 'preRenderViewElement'], [$this, 'elementPreRender']],
'#view' => $this->view,
];
}
......
......@@ -43,7 +43,7 @@ protected function setUp() {
*
* @see views_plugin_cache_time
*/
public function testTimeCaching() {
public function testTimeResultCaching() {
// Create a basic result which just 2 results.
$view = Views::getView('test_cache');
$view->setDisplay();
......@@ -88,7 +88,7 @@ public function testTimeCaching() {
*
* @see views_plugin_cache_time
*/
function testNoneCaching() {
function testNoneResultCaching() {
// Create a basic result which just 2 results.
$view = Views::getView('test_cache');
$view->setDisplay();
......@@ -203,4 +203,31 @@ public function testCacheData() {
}
}
/**
* Tests the output caching on an actual page.
*/
public function testCacheOutputOnPage() {
$view = Views::getView('test_display');
$view->storage->setStatus(TRUE);
$view->setDisplay('page_1');
$view->display_handler->overrideOption('cache', array(
'type' => 'time',
'options' => array(
'results_lifespan' => '3600',
'output_lifespan' => '3600'
)
));
$view->save();
$output_key = $view->getDisplay()->getPlugin('cache')->generateOutputKey();
$this->assertFalse(\Drupal::cache('render')->get($output_key));
$this->drupalGet('test-display');
$this->assertResponse(200);
$this->assertTrue(\Drupal::cache('render')->get($output_key));
$this->drupalGet('test-display');
$this->assertResponse(200);
$this->assertTrue(\Drupal::cache('render')->get($output_key));
}
}
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