Problem/Motivation
Trying to fix another issue I was debugging code in the 10.3.x/11.x feature "Expose all fields as blocks to Layout Builder". The FieldBlockDeriver->getFieldMap()
isn't cleaning up the array as effectively as it could be.
Steps to reproduce
Drupal\layout_builder\Plugin\Derivative\FieldBlockDeriver->getFieldMap(), line 198
has the following:
$field_map[$entity_type_id][$field_name]['bundles'] = array_intersect($field_info['bundles'], $layout_bundles[$entity_type_id]);
// If no bundles are using Layout Builder, remove this field from the
// field map.
if (empty($field_info['bundles'])) {
unset($field_map[$entity_type_id][$field_name]);
}
The comparison for `empty()` is not checking the array that has been altered, but instead checking the in memory version from when the array loop was created. Can be confirmed with debugging tools, or checking output of ->getFieldMap()
.
Proposed resolution
Replace empty($field_info['bundles'])
with empty($field_map[$entity_type_id][$field_name]['bundles'])
.
Remaining tasks
PR to follow.
User interface changes
None.
Introduced terminology
None.
API changes
->getFieldMap()
would return less items after the change, but the items would be the expected ones.
Data model changes
None.
Release notes snippet
Further reduced the automatically derived blocks for layout builder.