diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestComputedField.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestComputedField.php index 6cf03570..e87b41a8 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestComputedField.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestComputedField.php @@ -4,7 +4,10 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\Core\Field\FieldDefinition; +use Drupal\entity_test\FieldStorageDefinition; use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\entity_test\Plugin\Field\ComputedTestBundleFieldItemList; use Drupal\entity_test\Plugin\Field\ComputedReferenceTestFieldItemList; use Drupal\entity_test\Plugin\Field\ComputedTestCacheableStringItemList; use Drupal\entity_test\Plugin\Field\ComputedTestFieldItemList; @@ -59,4 +62,27 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { return $fields; } + /** + * {@inheritdoc} + */ + public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { + $fields = parent::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions); + + if ($bundle === $entity_type->id()) { + // @todo Use the proper FieldStorageDefinition class instead + // https://www.drupal.org/node/2280639. + $storageDefinition = FieldStorageDefinition::create('string') + ->setName('computed_bundle_field') + ->setTargetEntityTypeId($entity_type->id()) + ->setComputed(TRUE) + ->setClass(ComputedTestBundleFieldItemList::class); + $fields['computed_bundle_field'] = FieldDefinition::createFromFieldStorageDefinition($storageDefinition) + ->setLabel(t('A computed Bundle Field Test')) + ->setComputed(TRUE) + ->setClass(ComputedTestBundleFieldItemList::class); + } + + return $fields; + } + } diff --git a/core/modules/system/tests/modules/entity_test/src/EntityTestViewsData.php b/core/modules/system/tests/modules/entity_test/src/EntityTestViewsData.php index 27a1186a..9acddd62 100644 --- a/core/modules/system/tests/modules/entity_test/src/EntityTestViewsData.php +++ b/core/modules/system/tests/modules/entity_test/src/EntityTestViewsData.php @@ -25,6 +25,15 @@ public function getViewsData() { 'field_name' => 'computed_string_field', ], ]; + $views_data['entity_test_computed_field']['computed_bundle_field'] = [ + 'title' => $this->t('Computed Bundle Field'), + 'field' => [ + 'id' => 'field', + 'default_formatter' => 'string', + 'field_name' => 'computed_bundle_field', + 'bundles' => ['entity_test_computed_field'], + ], + ]; } if ($this->entityType->id() != 'entity_test') { diff --git a/core/modules/system/tests/modules/entity_test/src/Plugin/Field/ComputedTestBundleFieldItemList.php b/core/modules/system/tests/modules/entity_test/src/Plugin/Field/ComputedTestBundleFieldItemList.php new file mode 100644 index 00000000..877f7097 --- /dev/null +++ b/core/modules/system/tests/modules/entity_test/src/Plugin/Field/ComputedTestBundleFieldItemList.php @@ -0,0 +1,24 @@ +get('entity_test_computed_bundle_field_item_list_value', []) as $delta => $item) { + $this->list[$delta] = $this->createItem($delta, $item); + } + } + +} diff --git a/core/modules/views/src/Plugin/views/field/EntityField.php b/core/modules/views/src/Plugin/views/field/EntityField.php index 2c1d76ce..b58111d8 100644 --- a/core/modules/views/src/Plugin/views/field/EntityField.php +++ b/core/modules/views/src/Plugin/views/field/EntityField.php @@ -356,12 +356,25 @@ protected function getFieldStorageDefinition() { } // The list of field storage definitions above does not include computed - // base fields, so we need to explicitly fetch a list of all base fields in - // order to support them. + // fields, so we need to explicitly fetch a list of all fields in order to + // support them. If bundles were specified as part of the definition, check + // all the field definitions, else only base fields can be checked. // @see \Drupal\Core\Entity\EntityFieldManager::getFieldStorageDefinitions() - $base_fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); - if (isset($this->definition['field_name']) && isset($base_fields[$this->definition['field_name']])) { - return $base_fields[$this->definition['field_name']]->getFieldStorageDefinition(); + if (isset($this->definition['field_name'])) { + if (isset($this->definition['bundles'])) { + foreach ($this->definition['bundles'] as $bundle) { + $fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle); + if (isset($fields[$this->definition['field_name']])) { + return $fields[$this->definition['field_name']]->getFieldStorageDefinition(); + } + } + } + else { + $fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); + if (isset($fields[$this->definition['field_name']])) { + return $fields[$this->definition['field_name']]->getFieldStorageDefinition(); + } + } } } diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.computed_field_view.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.computed_field_view.yml index 501e1a53..1f146eeb 100644 --- a/core/modules/views/tests/modules/views_test_config/test_views/views.view.computed_field_view.yml +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.computed_field_view.yml @@ -138,6 +138,70 @@ display: field_api_classes: false entity_type: entity_test_computed_field plugin_id: field + computed_bundle_field: + id: computed_bundle_field + table: entity_test_computed_field + field: computed_bundle_field + relationship: none + group_type: group + admin_label: '' + label: '' + exclude: false + alter: + alter_text: false + text: '' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: true + ellipsis: true + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: false + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + click_sort_column: value + type: string + settings: + link_to_entity: false + group_column: value + group_columns: { } + group_rows: true + delta_limit: 0 + delta_offset: 0 + delta_reversed: false + delta_first_last: false + multi_type: separator + separator: ', ' + field_api_classes: false + entity_type: entity_test_computed_field + plugin_id: field filters: { } sorts: { } header: { } diff --git a/core/modules/views/tests/src/Kernel/Handler/ComputedFieldTest.php b/core/modules/views/tests/src/Kernel/Handler/ComputedFieldTest.php index a91c02d6..bc5e0038 100644 --- a/core/modules/views/tests/src/Kernel/Handler/ComputedFieldTest.php +++ b/core/modules/views/tests/src/Kernel/Handler/ComputedFieldTest.php @@ -42,6 +42,7 @@ protected function setUp($import_test_views = TRUE): void { */ public function testComputedFieldHandler() { \Drupal::state()->set('entity_test_computed_field_item_list_value', ['computed string']); + \Drupal::state()->set('entity_test_computed_bundle_field_item_list_value', ['some other string that is also computed']); $entity = EntityTestComputedField::create([]); $entity->save(); @@ -51,6 +52,7 @@ public function testComputedFieldHandler() { $rendered_view = $view->preview(); $output = $this->container->get('renderer')->renderRoot($rendered_view); $this->assertStringContainsString('computed string', (string) $output); + $this->assertStringContainsString('some other string that is also computed', (string) $output); } }