function FieldBlockDeriver::getFieldMap

Same name and namespace in other branches
  1. 10 core/modules/layout_builder/src/Plugin/Derivative/FieldBlockDeriver.php \Drupal\layout_builder\Plugin\Derivative\FieldBlockDeriver::getFieldMap()

Returns the entity field map for deriving block definitions.

Return value

array The entity field map.

See also

\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldMap()

1 call to FieldBlockDeriver::getFieldMap()
FieldBlockDeriver::getDerivativeDefinitions in core/modules/layout_builder/src/Plugin/Derivative/FieldBlockDeriver.php
Gets the definition of all derivatives of a base plugin.

File

core/modules/layout_builder/src/Plugin/Derivative/FieldBlockDeriver.php, line 168

Class

FieldBlockDeriver
Provides entity field block definitions for every field.

Namespace

Drupal\layout_builder\Plugin\Derivative

Code

protected function getFieldMap() : array {
    $field_map = $this->entityFieldManager
        ->getFieldMap();
    // If all fields are exposed as field blocks, just return the field map
    // without any further processing.
    if ($this->moduleHandler
        ->moduleExists('layout_builder_expose_all_field_blocks')) {
        return $field_map;
    }
    // Load all entity view displays which are using Layout Builder.
    
    /** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface[] $displays */
    $displays = $this->entityViewDisplayStorage
        ->loadByProperties([
        'third_party_settings.layout_builder.enabled' => TRUE,
    ]);
    $layout_bundles = [];
    foreach ($displays as $display) {
        $bundle = $display->getTargetBundle();
        $layout_bundles[$display->getTargetEntityTypeId()][$bundle] = $bundle;
    }
    // Process $field_map, removing any entity types which are not using Layout
    // Builder.
    $field_map = array_intersect_key($field_map, $layout_bundles);
    foreach ($field_map as $entity_type_id => $fields) {
        foreach ($fields as $field_name => $field_info) {
            $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]);
            }
        }
    }
    return $field_map;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.