Commit e8a4f8bc authored by catch's avatar catch

Issue #2616816 by dawehner, Jaesin: Views aggregation: Grouping a field that...

Issue #2616816 by dawehner, Jaesin: Views aggregation: Grouping a field that doesn't exist on all bundles causes an error
parent ac8b1fa4
...@@ -820,9 +820,10 @@ public function getItems(ResultRow $values) { ...@@ -820,9 +820,10 @@ public function getItems(ResultRow $values) {
'settings' => $this->options['settings'], 'settings' => $this->options['settings'],
'label' => 'hidden', 'label' => 'hidden',
]; ];
$build_list = $this->createEntityForGroupBy($this->getEntity($values), $values) // Some bundles might not have a specific field, in which case the faked
->{$this->definition['field_name']} // entity doesn't have it either.
->view($display); $entity = $this->createEntityForGroupBy($this->getEntity($values), $values);
$build_list = isset($entity->{$this->definition['field_name']}) ? $entity->{$this->definition['field_name']}->view($display) : NULL;
} }
if (!$build_list) { if (!$build_list) {
...@@ -1002,8 +1003,17 @@ protected function getTableMapping() { ...@@ -1002,8 +1003,17 @@ protected function getTableMapping() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getValue(ResultRow $values, $field = NULL) { public function getValue(ResultRow $values, $field = NULL) {
$entity = $this->getEntity($values);
// Some bundles might not have a specific field, in which case the entity
// (potentially a fake one) doesn't have it either.
/** @var \Drupal\Core\Field\FieldItemListInterface $field_item_list */ /** @var \Drupal\Core\Field\FieldItemListInterface $field_item_list */
$field_item_list = $this->getEntity($values)->{$this->definition['field_name']}; $field_item_list = isset($entity->{$this->definition['field_name']}) ? $entity->{$this->definition['field_name']} : NULL;
if (!isset($field_item_list)) {
// There isn't anything we can do without a valid field.
return NULL;
}
$field_item_definition = $field_item_list->getFieldDefinition(); $field_item_definition = $field_item_list->getFieldDefinition();
if ($field_item_definition->getFieldStorageDefinition()->getCardinality() == 1) { if ($field_item_definition->getFieldStorageDefinition()->getCardinality() == 1) {
......
...@@ -25,7 +25,7 @@ class QueryGroupByTest extends ViewKernelTestBase { ...@@ -25,7 +25,7 @@ class QueryGroupByTest extends ViewKernelTestBase {
* *
* @var array * @var array
*/ */
public static $testViews = array('test_group_by_in_filters', 'test_aggregate_count', 'test_group_by_count', 'test_group_by_count_multicardinality'); public static $testViews = array('test_group_by_in_filters', 'test_aggregate_count', 'test_group_by_count', 'test_group_by_count_multicardinality', 'test_group_by_field_not_within_bundle');
/** /**
* Modules to enable. * Modules to enable.
...@@ -292,4 +292,48 @@ public function testGroupByFieldWithCardinality() { ...@@ -292,4 +292,48 @@ public function testGroupByFieldWithCardinality() {
$this->assertEqual('6', $view->getStyle()->getField(5, 'field_test')); $this->assertEqual('6', $view->getStyle()->getField(5, 'field_test'));
} }
/**
* Tests groupby with a field not existing on some bundle.
*/
public function testGroupByWithFieldsNotExistingOnBundle() {
$field_storage = FieldStorageConfig::create([
'type' => 'integer',
'field_name' => 'field_test',
'cardinality' => 4,
'entity_type' => 'entity_test_mul',
]);
$field_storage->save();
$field = FieldConfig::create([
'field_name' => 'field_test',
'entity_type' => 'entity_test_mul',
'bundle' => 'entity_test_mul',
]);
$field->save();
$entities = [];
$entity = EntityTestMul::create([
'field_test' => [1],
'type' => 'entity_test_mul',
]);
$entity->save();
$entities[] = $entity;
$entity = EntityTestMul::create([
'type' => 'entity_test_mul2',
]);
$entity->save();
$entities[] = $entity;
$view = Views::getView('test_group_by_field_not_within_bundle');
$this->executeView($view);
$this->assertEqual(2, count($view->result));
// The first result is coming from entity_test_mul2, so no field could be
// rendered.
$this->assertEqual('', $view->getStyle()->getField(0, 'field_test'));
// The second result is coming from entity_test_mul, so its field value
// could be rendered.
$this->assertEqual('1', $view->getStyle()->getField(1, 'field_test'));
}
} }
langcode: en
status: true
dependencies: { }
id: test_group_by_field_not_within_bundle
label: ''
module: views
description: ''
tag: ''
base_table: entity_test_mul_property_data
base_field: id
core: '8'
display:
default:
display_options:
access:
type: none
cache:
type: tag
exposed_form:
type: basic
fields:
field_test:
alter:
alter_text: false
ellipsis: true
html: false
make_link: false
strip_tags: false
trim: false
word_boundary: true
group_type: group
group_column: value
empty_zero: false
field: field_test
hide_empty: false
id: field_test
table: entity_test_mul__field_test
entity_type: entity_test_mul
entity_field: field_test
plugin_id: field
sorts:
field_test_value:
table: entity_test_mul__field_test
field: field_test_value
id: field_test_value
entity_type: entity_test_mul
entity_field: field_test
group_type: group
order: ASC
plugin_id: standard
group_by: true
pager:
type: some
style:
type: default
row:
type: fields
display_plugin: default
display_title: Master
id: default
position: 0
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