Issue #2981047: Views does not support computed bundle fields
Merge request reports
Activity
59 64 return $fields; 60 65 } 61 66 67 /** 68 * {@inheritdoc} 69 */ 70 public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { 71 $fields = parent::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions); 72 73 $computed_field_bundles = [ 74 'entity_test_comp_field_bundle', 75 'entity_test_comp_field_bundle_2', 76 ]; 77 78 if (array_search($bundle, $computed_field_bundles) !== FALSE) { changed this line in version 3 of the diff
354 // base fields, so we need to explicitly fetch a list of all base fields in 355 // order to support them. 354 // fields, so we need to explicitly fetch a list of all fields in order to 355 // support them. If bundles were specified as part of the definition, check 356 // all the field definitions, else only base fields can be checked. 356 357 // @see \Drupal\Core\Entity\EntityFieldManager::getFieldStorageDefinitions() 357 $base_fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); 358 if (isset($this->definition['field_name']) && isset($base_fields[$this->definition['field_name']])) { 359 return $base_fields[$this->definition['field_name']]->getFieldStorageDefinition(); 358 if (isset($this->definition['field_name'])) { 359 if (isset($this->definition['bundles'])) { 360 foreach ($this->definition['bundles'] as $bundle) { 361 $fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle); 362 if (isset($fields[$this->definition['field_name']])) { 363 return $fields[$this->definition['field_name']]->getFieldStorageDefinition(); 364 } - Comment on lines +361 to +364
So what happens here if the definition varies between bundles? We only consider the first matching one - but there's nothing to say bundle one can't have a different computed field than bundle two, but share the same name - it's a common use case for bundles - modelling different behaviours.
We don't have test coverage for this scenario either.
I'm not really sure how we can get around this, the context is missing to be able to solve this cleanly.
I think the only thing we can do is get the definition for all bundles and make sure they all have the same computed class and if not raise an exception or warning or similar? There's just not enough context to do anything else.
Ideally we could do that at the time we collate views data rather than here in run-time code. Perhaps \Drupal\views\ViewsData::processEntityTypes is a good place to do that - thoughts @Lendude
changed this line in version 3 of the diff
352 352 353 353 // The list of field storage definitions above does not include computed 354 // base fields, so we need to explicitly fetch a list of all base fields in 355 // order to support them. 354 // fields, so we need to explicitly fetch a list of all fields in order to 355 // support them. If bundles were specified as part of the definition, check 356 // all the field definitions, else only base fields can be checked. 356 357 // @see \Drupal\Core\Entity\EntityFieldManager::getFieldStorageDefinitions() 357 $base_fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); 358 if (isset($this->definition['field_name']) && isset($base_fields[$this->definition['field_name']])) { 359 return $base_fields[$this->definition['field_name']]->getFieldStorageDefinition(); 358 if (isset($this->definition['field_name'])) { 359 if (isset($this->definition['bundles'])) { 360 foreach ($this->definition['bundles'] as $bundle) { 361 $fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle); 362 if (isset($fields[$this->definition['field_name']])) { changed this line in version 3 of the diff
357 $base_fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); 358 if (isset($this->definition['field_name']) && isset($base_fields[$this->definition['field_name']])) { 359 return $base_fields[$this->definition['field_name']]->getFieldStorageDefinition(); 358 if (isset($this->definition['field_name'])) { 359 if (isset($this->definition['bundles'])) { 360 foreach ($this->definition['bundles'] as $bundle) { 361 $fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle); 362 if (isset($fields[$this->definition['field_name']])) { 363 return $fields[$this->definition['field_name']]->getFieldStorageDefinition(); 364 } 365 } 366 } 367 else { 368 $fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); 369 if (isset($fields[$this->definition['field_name']])) { 370 return $fields[$this->definition['field_name']]->getFieldStorageDefinition(); added 176 commits
-
b4181de5...a642dc22 - 175 commits from branch
project:9.5.x
- c6bcba1b - Merge remote-tracking branch 'origin/9.5.x' into 2981047-views-computed-bundle-fields-9-5
-
b4181de5...a642dc22 - 175 commits from branch