Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jucedogi/05ef88974f78e346d017d51168f2498b to your computer and use it in GitHub Desktop.
Save jucedogi/05ef88974f78e346d017d51168f2498b to your computer and use it in GitHub Desktop.
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 0000000000..877f7097e5
--- /dev/null
+++ b/core/modules/system/tests/modules/entity_test/src/Plugin/Field/ComputedTestBundleFieldItemList.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Drupal\entity_test\Plugin\Field;
+
+use Drupal\Core\TypedData\ComputedItemListTrait;
+use Drupal\Core\Field\FieldItemList;
+
+/**
+ * A computed field item list for a bundle field.
+ */
+class ComputedTestBundleFieldItemList extends FieldItemList {
+
+ use ComputedItemListTrait;
+
+ /**
+ * Compute the list property from state.
+ */
+ protected function computeValue() {
+ foreach (\Drupal::state()->get('entity_test_computed_bundle_field_item_list_value', []) as $delta => $item) {
+ $this->list[$delta] = $this->createItem($delta, $item);
+ }
+ }
+
+}
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 27a1186ada..9acddd62a3 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/views/src/Plugin/views/field/EntityField.php b/core/modules/views/src/Plugin/views/field/EntityField.php
index 95d1ebde90..c9ad5c8ea3 100644
--- a/core/modules/views/src/Plugin/views/field/EntityField.php
+++ b/core/modules/views/src/Plugin/views/field/EntityField.php
@@ -347,12 +347,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 501e1a53d8..1f146eebab 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 5b1e81b99c..c588ef967b 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);
}
}
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
--- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestComputedField.php
+++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestComputedField.php (date 1646764271196)
@@ -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;
@@ -56,6 +59,29 @@
->setReadOnly(FALSE)
->setInternal(FALSE);
+ 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment