function EntityFieldManager::buildBundleFieldDefinitions

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityFieldManager.php \Drupal\Core\Entity\EntityFieldManager::buildBundleFieldDefinitions()
  2. 10 core/lib/Drupal/Core/Entity/EntityFieldManager.php \Drupal\Core\Entity\EntityFieldManager::buildBundleFieldDefinitions()
  3. 11.x core/lib/Drupal/Core/Entity/EntityFieldManager.php \Drupal\Core\Entity\EntityFieldManager::buildBundleFieldDefinitions()

Builds field definitions for a specific bundle within an entity type.

Parameters

string $entity_type_id: The entity type ID. Only entity types that implement \Drupal\Core\Entity\FieldableEntityInterface are supported.

string $bundle: The bundle.

\Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions: The list of base field definitions.

Return value

\Drupal\Core\Field\FieldDefinitionInterface[] An array of bundle field definitions, keyed by field name. Does not include base fields.

1 call to EntityFieldManager::buildBundleFieldDefinitions()
EntityFieldManager::getFieldDefinitions in core/lib/Drupal/Core/Entity/EntityFieldManager.php
Gets the field definitions for a specific bundle.

File

core/lib/Drupal/Core/Entity/EntityFieldManager.php, line 365

Class

EntityFieldManager
Manages the discovery of entity fields.

Namespace

Drupal\Core\Entity

Code

protected function buildBundleFieldDefinitions($entity_type_id, $bundle, array $base_field_definitions) {
    $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
    $class = $entity_type->getClass();
    // Allow the entity class to provide bundle fields and bundle-specific
    // overrides of base fields.
    $bundle_field_definitions = $class::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions);
    // Load base field overrides from configuration. These take precedence over
    // base field overrides returned above.
    $base_field_override_ids = array_map(function ($field_name) use ($entity_type_id, $bundle) {
        return $entity_type_id . '.' . $bundle . '.' . $field_name;
    }, array_keys($base_field_definitions));
    $base_field_overrides = $this->entityTypeManager
        ->getStorage('base_field_override')
        ->loadMultiple($base_field_override_ids);
    foreach ($base_field_overrides as $base_field_override) {
        
        /** @var \Drupal\Core\Field\Entity\BaseFieldOverride $base_field_override */
        $field_name = $base_field_override->getName();
        $bundle_field_definitions[$field_name] = $base_field_override;
    }
    $provider = $entity_type->getProvider();
    foreach ($bundle_field_definitions as $definition) {
        // @todo Remove this check once FieldDefinitionInterface exposes a proper
        //  provider setter. See https://www.drupal.org/node/2225961.
        if ($definition instanceof BaseFieldDefinition) {
            $definition->setProvider($provider);
        }
    }
    // Retrieve base field definitions from modules.
    foreach ($this->moduleHandler
        ->getImplementations('entity_bundle_field_info') as $module) {
        $module_definitions = $this->moduleHandler
            ->invoke($module, 'entity_bundle_field_info', [
            $entity_type,
            $bundle,
            $base_field_definitions,
        ]);
        if (!empty($module_definitions)) {
            // Ensure the provider key actually matches the name of the provider
            // defining the field.
            foreach ($module_definitions as $field_name => $definition) {
                // @todo Remove this check once FieldDefinitionInterface exposes a
                //  proper provider setter. See https://www.drupal.org/node/2225961.
                if ($definition instanceof BaseFieldDefinition) {
                    $definition->setProvider($module);
                }
                $bundle_field_definitions[$field_name] = $definition;
            }
        }
    }
    // Automatically set the field name, target entity type and bundle
    // for non-configurable fields.
    foreach ($bundle_field_definitions as $field_name => $field_definition) {
        if ($field_definition instanceof BaseFieldDefinition) {
            $field_definition->setName($field_name);
            $field_definition->setTargetEntityTypeId($entity_type_id);
        }
        if ($field_definition instanceof BaseFieldDefinition || $field_definition instanceof FieldDefinition) {
            $field_definition->setTargetBundle($bundle);
        }
    }
    // Invoke 'per bundle' alter hook.
    $this->moduleHandler
        ->alter('entity_bundle_field_info', $bundle_field_definitions, $entity_type, $bundle);
    return $bundle_field_definitions;
}

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