Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
project
drupal
Commits
8ddd1342
Commit
8ddd1342
authored
Jul 27, 2015
by
alexpott
Browse files
Issue
#2534780
by olli, dawehner: Fatal error rendering fields using an optional relationship
parent
b116b581
Changes
3
Hide whitespace changes
Inline
Side-by-side
core/modules/views/src/Entity/Render/EntityFieldRenderer.php
View file @
8ddd1342
...
...
@@ -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
{
else
if
(
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
...
...
core/modules/views/src/Plugin/views/field/FieldPluginBase.php
View file @
8ddd1342
...
...
@@ -395,7 +395,7 @@ public function getEntity(ResultRow $values) {
if
(
$relationship_id
==
'none'
)
{
return
$values
->
_entity
;
}
else
{
else
if
(
isset
(
$values
->
_relationship_entities
[
$relationship_id
]))
{
return
$values
->
_relationship_entities
[
$relationship_id
];
}
}
...
...
core/modules/views/src/Tests/Handler/RelationshipTest.php
View file @
8ddd1342
...
...
@@ -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'
=>
''
])));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment