function ExtraFieldBlockDeriver::getDerivativeDefinitions

Same name in other branches
  1. 9 core/modules/layout_builder/src/Plugin/Derivative/ExtraFieldBlockDeriver.php \Drupal\layout_builder\Plugin\Derivative\ExtraFieldBlockDeriver::getDerivativeDefinitions()
  2. 8.9.x core/modules/layout_builder/src/Plugin/Derivative/ExtraFieldBlockDeriver.php \Drupal\layout_builder\Plugin\Derivative\ExtraFieldBlockDeriver::getDerivativeDefinitions()
  3. 11.x core/modules/layout_builder/src/Plugin/Derivative/ExtraFieldBlockDeriver.php \Drupal\layout_builder\Plugin\Derivative\ExtraFieldBlockDeriver::getDerivativeDefinitions()

Overrides DeriverBase::getDerivativeDefinitions

File

core/modules/layout_builder/src/Plugin/Derivative/ExtraFieldBlockDeriver.php, line 99

Class

ExtraFieldBlockDeriver
Provides entity field block definitions for every field.

Namespace

Drupal\layout_builder\Plugin\Derivative

Code

public function getDerivativeDefinitions($base_plugin_definition) {
    $entity_type_labels = $this->entityTypeRepository
        ->getEntityTypeLabels();
    $enabled_bundle_ids = $this->bundleIdsWithLayoutBuilderDisplays();
    $expose_all_fields = $this->moduleHandler
        ->moduleExists('layout_builder_expose_all_field_blocks');
    foreach ($this->entityTypeManager
        ->getDefinitions() as $entity_type_id => $entity_type) {
        // Only process fieldable entity types.
        if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
            continue;
        }
        // If not loading everything, skip entity types that aren't included.
        if (!$expose_all_fields && !isset($enabled_bundle_ids[$entity_type_id])) {
            continue;
        }
        $bundles = $this->entityTypeBundleInfo
            ->getBundleInfo($entity_type_id);
        foreach ($bundles as $bundle_id => $bundle) {
            // If not loading everything, skip bundle types that aren't included.
            if (!$expose_all_fields && !isset($enabled_bundle_ids[$entity_type_id][$bundle_id])) {
                continue;
            }
            $extra_fields = $this->entityFieldManager
                ->getExtraFields($entity_type_id, $bundle_id);
            // Skip bundles without any extra fields.
            if (empty($extra_fields['display'])) {
                continue;
            }
            foreach ($extra_fields['display'] as $extra_field_id => $extra_field) {
                $derivative = $base_plugin_definition;
                $derivative['category'] = $this->t('@entity fields', [
                    '@entity' => $entity_type_labels[$entity_type_id],
                ]);
                $derivative['admin_label'] = $extra_field['label'];
                $context_definition = EntityContextDefinition::fromEntityType($entity_type)->addConstraint('Bundle', [
                    $bundle_id,
                ]);
                $derivative['context_definitions'] = [
                    'entity' => $context_definition,
                ];
                $derivative_id = $entity_type_id . PluginBase::DERIVATIVE_SEPARATOR . $bundle_id . PluginBase::DERIVATIVE_SEPARATOR . $extra_field_id;
                $this->derivatives[$derivative_id] = $derivative;
            }
        }
    }
    return $this->derivatives;
}

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