Adds computed bundle basefield support in views
Closes #2981047
Merge request reports
Activity
added 557 commits
-
a8c7a63d...57d03b6b - 556 commits from branch
project:10.0.x
- 9ac1d1af - Adds computed bundle basefield support in views
-
a8c7a63d...57d03b6b - 556 commits from branch
added 218 commits
-
9ac1d1af...66e81d7e - 215 commits from branch
project:10.0.x
- a8c7a63d - Adds computed bundle basefield support in views
- 2a256469 - Merge remote-tracking branch 'origin/10.0.x' into 2981047-views-computed-bundle-fields-10-0
- 4ba274e7 - Merge branch '2981047-views-computed-bundle-fields-10-0' of...
Toggle commit list-
9ac1d1af...66e81d7e - 215 commits from branch
added 5 commits
-
4ba274e7...85c87c70 - 2 commits from branch
project:10.0.x
- 2e4fda6a - Merge remote-tracking branch 'origin/10.0.x' into 2981047-views-computed-bundle-fields-10-0
- b2a0b5ea - Updates from reviews
- d08a54e6 - Simplify the return statement for D10
Toggle commit list-
4ba274e7...85c87c70 - 2 commits from branch
added 1 commit
- 67abeb3c - Revert "Simplify the return statement for D10"
added 7 commits
-
67abeb3c...bd3a6009 - 6 commits from branch
project:10.0.x
- 4db83b9a - Merge remote-tracking branch 'origin/10.0.x' into 2981047-views-computed-bundle-fields-10-0
-
67abeb3c...bd3a6009 - 6 commits from branch
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 (in_array($bundle, $computed_field_bundles) !== FALSE) { 23 26 * "id" = "id", 24 27 * "uuid" = "uuid", 25 28 * "label" = "name", 29 * "bundle" = "type", 360 // support them. If bundles were specified as part of the definition, check 361 // all the field definitions, else only base fields can be checked. 361 362 // @see \Drupal\Core\Entity\EntityFieldManager::getFieldStorageDefinitions() 362 $base_fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); 363 if (isset($this->definition['field_name']) && isset($base_fields[$this->definition['field_name']])) { 364 return $base_fields[$this->definition['field_name']]->getFieldStorageDefinition(); 363 if (!isset($this->definition['field_name'])) { 364 return NULL; 365 } 366 if (isset($this->definition['bundles'])) { 367 foreach ($this->definition['bundles'] as $bundle) { 368 $fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle); 369 if (isset($fields[$this->definition['field_name']])) { 370 return $fields[$this->definition['field_name']]->getFieldStorageDefinition(); 371 } 372 } - Comment on lines +367 to +372
I still think we've got the same issue as https://www.drupal.org/project/drupal/issues/2981047#comment-14625096 which I think the idea in comment 108 would address
changed this line in version 8 of the diff
356 356 } 357 357 358 358 // The list of field storage definitions above does not include computed 359 // base fields, so we need to explicitly fetch a list of all base fields in 360 // order to support them. 359 // fields, so we need to explicitly fetch a list of all fields in order to 360 // support them. If bundles were specified as part of the definition, check 361 // all the field definitions, else only base fields can be checked. 361 362 // @see \Drupal\Core\Entity\EntityFieldManager::getFieldStorageDefinitions() 362 $base_fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); 363 if (isset($this->definition['field_name']) && isset($base_fields[$this->definition['field_name']])) { 364 return $base_fields[$this->definition['field_name']]->getFieldStorageDefinition(); 363 if (!isset($this->definition['field_name'])) { 364 return NULL; 365 } 366 if (isset($this->definition['bundles'])) { On second thoughts, I don't think we need this at all! The only reason we have the 'bundles' in the definition is so that we can get a field definition. We loop over each bundle, and take the first one. We don't actually care WHICH bundles it's on. So, we could more easily loop over ALL bundles, and take the first one where we find the field. Or we can consult the field map, and take the first one.
We're using the presence of 'bundles' to detect whether this is a computed field, but it would be simpler to check for base fields first, and then fall back to bundle fields.
I've been working on this in DrupalCon Pittsburgh, and I've managed to resolve pretty much all the other comments - including creating a new entity type for computed bundle base fields.
However, the field map does not appear to contain the bundle base fields as defined in my entity's "bundleFieldDefinitions()".
They are visible if I use the EntityFieldManager::getFieldDefinitions() but not in the map.
I'm not sure if this is expected due to the way the map is cached only on create and delete, but I just do not seem to be able to get these bundle fields to appear. Base fields appear fine, but not bundle base fields.
I do not have access to the EntityTypeBundleInfo service within the EntityField class to determine the bundles available for the entity.
So that leaves me with a couple of options:
-
I do not remove the 'bundles' key on the EntityViewsData definition, as I was doing before, and load the first field definition from the EntityFieldManager.
-
I add the EntityTypeBundleInfo service with dependency injection to the EntityField views class.
Thoughts on the direction?
Edited by Owen Bush-
changed this line in version 8 of the diff
363 if (isset($this->definition['field_name']) && isset($base_fields[$this->definition['field_name']])) { 364 return $base_fields[$this->definition['field_name']]->getFieldStorageDefinition(); 363 if (!isset($this->definition['field_name'])) { 364 return NULL; 365 } 366 if (isset($this->definition['bundles'])) { 367 foreach ($this->definition['bundles'] as $bundle) { 368 $fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle); 369 if (isset($fields[$this->definition['field_name']])) { 370 return $fields[$this->definition['field_name']]->getFieldStorageDefinition(); 371 } 372 } 373 return NULL; 374 } 375 376 $fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id);