Commit 47b7c006 authored by catch's avatar catch

Issue #2157777 by damiankloip, dawehner: Views results cache has full entities in it

parent a9b064cc
......@@ -136,7 +136,7 @@ public function cacheSet($type) {
break;
case 'results':
$data = array(
'result' => $this->view->result,
'result' => $this->prepareViewResult($this->view->result),
'total_rows' => isset($this->view->total_rows) ? $this->view->total_rows : 0,
'current_page' => $this->view->getCurrentPage(),
);
......@@ -167,6 +167,8 @@ public function cacheGet($type) {
if ($cache = \Drupal::cache($this->resultsBin)->get($this->generateResultsKey())) {
if (!$cutoff || $cache->created > $cutoff) {
$this->view->result = $cache->data['result'];
// Load entities for each result.
$this->view->query->loadEntities($this->view->result);
$this->view->total_rows = $cache->data['total_rows'];
$this->view->setCurrentPage($cache->data['current_page']);
$this->view->execute_time = 0;
......@@ -340,6 +342,29 @@ protected function getCacheTags() {
return $tags;
}
/**
* Prepares the view result before putting it into cache.
*
* @param \Drupal\views\ResultRow[] $result
* The result containing loaded entities.
*
* @return \Drupal\views\ResultRow[] $result
* The result without loaded entities.
*/
protected function prepareViewResult(array $result) {
$return = [];
// Clone each row object and remove any loaded entities, to keep the
// original result rows intact.
foreach ($result as $key => $row) {
$clone = clone $row;
$clone->resetEntityData();
$return[$key] = $clone;
}
return $return;
}
/**
* Alters the cache metadata of a display upon saving a view.
*
......
......@@ -24,7 +24,7 @@ class ResultRow {
*
* @var \Drupal\Core\Entity\EntityInterface[]
*/
public $_relationship_entities = array();
public $_relationship_entities = [];
/**
* An incremental number which represents the row in the entire result.
......@@ -45,4 +45,12 @@ public function __construct(array $values = array()) {
}
}
/**
* Resets the _entity and _relationship_entities properties.
*/
public function resetEntityData() {
$this->_entity = NULL;
$this->_relationship_entities = [];
}
}
......@@ -23,7 +23,7 @@ class CacheTest extends PluginTestBase {
*
* @var array
*/
public static $testViews = array('test_view', 'test_cache', 'test_groupwise_term_ui');
public static $testViews = array('test_view', 'test_cache', 'test_groupwise_term_ui', 'test_display');
/**
* Modules to enable.
......@@ -166,7 +166,41 @@ public function testSubqueryStringCache() {
$cid = 'views_relationship_groupwise_max:test_groupwise_term_ui:default:tid_representative';
$cache = \Drupal::cache('data')->get($cid);
$this->assertEqual($cid, $cache->cid, 'Subquery String cached as expected.');
}
/**
* Tests the data contained in cached items.
*/
public function testCacheData() {
for ($i = 1; $i <= 5; $i++) {
$this->drupalCreateNode();
}
$view = Views::getView('test_display');
$view->setDisplay();
$view->display_handler->overrideOption('cache', array(
'type' => 'time',
'options' => array(
'results_lifespan' => '3600',
'output_lifespan' => '3600'
)
));
$this->executeView($view);
// Get the cache item.
$cid = $view->display_handler->getPlugin('cache')->generateResultsKey();
$cache = \Drupal::cache('data')->get($cid);
// Assert there are results, empty results would mean this test case would
// pass otherwise.
$this->assertTrue(count($cache->data['result']), 'Results saved in cached data.');
// Assert each row doesn't contain '_entity' or '_relationship_entities'
// items.
foreach ($cache->data['result'] as $row) {
$this->assertIdentical($row->_entity, NULL, 'Cached row "_entity" property is NULL');
$this->assertIdentical($row->_relationship_entities, [], 'Cached row "_relationship_entities" property is empty');
}
}
}
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