Commit 8ddd1342 authored by alexpott's avatar alexpott

Issue #2534780 by olli, dawehner: Fatal error rendering fields using an optional relationship

parent b116b581
......@@ -138,13 +138,18 @@ public function render(ResultRow $row, Field $field = NULL) {
$build = $this->build[$row->index][$field_id];
unset($this->build[$row->index][$field_id]);
}
else {
elseif (isset($this->build[$row->index])) {
// In the uncommon case where a field gets rendered several times
// (typically through direct Views API calls), the pre-computed render
// array was removed by the unset() above. We have to manually rebuild
// the render array for the row.
$build = $this->buildFields([$row])[$row->index][$field_id];
}
else {
// In case the relationship is optional, there might not be any fields
// to render for this row.
$build = [];
}
}
else {
// Same logic as above, in the case where we are being called for a whole
......@@ -199,8 +204,9 @@ protected function buildFields(array $values) {
$entities_by_bundles = [];
$field = $this->view->field[current($field_ids)];
foreach ($values as $result_row) {
$entity = $field->getEntity($result_row);
$entities_by_bundles[$entity->bundle()][$result_row->index] = $this->getEntityTranslation($entity, $result_row);
if ($entity = $field->getEntity($result_row)) {
$entities_by_bundles[$entity->bundle()][$result_row->index] = $this->getEntityTranslation($entity, $result_row);
}
}
// Determine unique sets of fields that can be processed by the same
......
......@@ -395,7 +395,7 @@ public function getEntity(ResultRow $values) {
if ($relationship_id == 'none') {
return $values->_entity;
}
else {
elseif (isset($values->_relationship_entities[$relationship_id])) {
return $values->_relationship_entities[$relationship_id];
}
}
......
......@@ -7,8 +7,8 @@
namespace Drupal\views\Tests\Handler;
use Drupal\simpletest\UserCreationTrait;
use Drupal\views\Views;
use Drupal\views\Tests\ViewUnitTestBase;
use Drupal\views\Tests\Plugin\RelationshipJoinTestBase;
/**
......@@ -18,6 +18,7 @@
* @see \Drupal\views\Plugin\views\relationship\RelationshipPluginBase
*/
class RelationshipTest extends RelationshipJoinTestBase {
use UserCreationTrait;
/**
* Views used by this test.
......@@ -133,4 +134,51 @@ public function testRelationshipQuery() {
$this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
}
/**
* Tests rendering of a view with a relationship.
*/
public function testRelationshipRender() {
$author1 = $this->createUser();
db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 1", [':uid' => $author1->id()]);
$author2 = $this->createUser();
db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 2", [':uid' => $author2->id()]);
// Set uid to non-existing author uid for row 3.
db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 3", [':uid' => $author2->id() + 123]);
$view = Views::getView('test_view');
// Add a relationship for authors.
$view->getDisplay()->overrideOption('relationships', [
'uid' => [
'id' => 'uid',
'table' => 'views_test_data',
'field' => 'uid',
],
]);
// Add fields for {views_test_data}.id and author name.
$view->getDisplay()->overrideOption('fields', [
'id' => [
'id' => 'id',
'table' => 'views_test_data',
'field' => 'id',
],
'author' => [
'id' => 'author',
'table' => 'users_field_data',
'field' => 'name',
'relationship' => 'uid',
],
]);
// Render the view.
$output = $view->preview();
$html = $this->container->get('renderer')->renderRoot($output);
$this->setRawContent($html);
// Check that the output contains correct values.
$xpath = '//div[@class="views-row" and div[@class="views-field views-field-id"]=:id and div[@class="views-field views-field-author"]=:author]';
$this->assertEqual(1, count($this->xpath($xpath, [':id' => 1, ':author' => $author1->getUsername()])));
$this->assertEqual(1, count($this->xpath($xpath, [':id' => 2, ':author' => $author2->getUsername()])));
$this->assertEqual(1, count($this->xpath($xpath, [':id' => 3, ':author' => ''])));
}
}
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