Problem:
Every computed field added to a bundle using hook_entity_bundle_field_info() will be ignored by the FieldBlockDeriver, and computed fields will not be listed in the content fields section when editing the layout of the current bundle type.
Causes:
The getDerivativeDefinitions method of Drupal\layout_builder\Plugin\Derivative\FieldBlockDeriver constructs its field_map for a given entity type using getFieldMap wich doesn't trigger every fields related hooks.
Later in the code, the method getFieldDefinitions($entity_type_id, $bundle) which triggers all fields related hooks is used, but the results of this method are narrowed to previousey listed fields only
$field_definition = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle)[$field_name];
Possible Fix:
Complete the entity_field_map, with fields declared using hook_entity_bundle_field_info()
Possible other problem:
There could be the same issue for extra fields declared using hook_entity_extra_field_info()
| Comment | File | Size | Author |
|---|---|---|---|
| #21 | layoutbuilder-bundlecomputedfieldsfix-3034979-21-11.x.patch | 3.9 KB | claudiu.cristea |
Comments
Comment #2
muldos commentedpatches provided
Comment #3
muldos commentedfix the 8.6 patch
Comment #4
tim.plunkettThanks for tracking this down.
I'm of the opinion that this is a bug in getFieldMap() itself, not something that LB should be handling itself.
Comment #5
muldos commentedI agree that if getFieldMap() was calling every fields related hooks, it would solve the problem too.
But getFieldMap could have been written the way it is by design as a comment suggest
(https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21...)
So maybe author(s) of EntityFieldManager were considering that to get a comprehensive list of per bundle fields, other methods should be used.
Also as the default display manager handles bundle computed fields properly because it is using getFieldDefinitions($entity_type_id, $bundle) observing fields disappeared when switching to layout builder can be seen as a layout builder's bug by the end users, and that's also why I have done the fix here.
Comment #6
plachIt seems to me that we have potentially two issues here:
EntityFieldManager::getFieldMap()should definitely include definitions provided throughhook_entity_bundle_field_info(). I'm wondering whether the issue is that computed issues are not "created" and thus not registered in theentity.definitions.bundle_field_mapkey-value collection.hook_entity_extra_field_info()are definitely being left out here. I'm not sure whether this is by design but I guess this could represent a UX issue, since site builders might not be able to tell the difference between a regular field and an extra field.Comment #8
tim.plunkettAs I said in #4, I think there is a bug in getFieldMap, see also #2985882: Workaround for "Call to a member function getLabel() after enabling layout_builder"
Re #6.2 there is also now an ExtraFieldBlockDeriver which is separate.
Comment #17
musa.thomasany update about this ?
I do a test and extra field work perfectly but computed field are stille not available inside layout builder
Comment #18
claudiu.cristeaAs there is an ExtraFieldBlockDeriver why not a new ComputedFieldBlockDeriver? Because I think there are zero chances that
::getFieldMap()to be fixedComment #19
crzdev commentedI would prefer to have a single collector to avoid the possibility of having different ids in case it is finally integrated into the getFieldMap method, in addition to not making the editing to editors more complex than it already is (by having another different category).
If that happens, as code makes an array merge, i think no other change will be required than removing deprecated code (no massive update should be necessary, in any case, display, overrides & other like section library...).
I've been trying to use "hook_entity_field_storage_info", "hook_entity_bundle_field_info" && "\Drupal::service('field_definition.listener')->onFieldDefinitionCreate($bundle_definition);" into "hook_entity_bundle_create" & this patch is no necessary but that generates other problems like field table removed when a bundle field is used more than once. That was based into "entity_schema_test_entity_field_storage_info".
We have been using #patch with core ^9.5 && ^10 for a while & is working perfectly (VLSuite). So only pending step whould be test coverage.
Comment #20
wim leersWhy do we think we cannot fix
::getFieldMap()🤔Let's fix that in #3045509: EntityFieldManager key/value field map gets out of sync, doesn't recognise bundle fields 😊.
Comment #21
claudiu.cristeaPatch for 11.x