function EntityReferenceItem::calculateDependencies

Same name in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::calculateDependencies()
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::calculateDependencies()
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::calculateDependencies()

Overrides FieldItemBase::calculateDependencies

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php, line 466

Class

EntityReferenceItem
Defines the 'entity_reference' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
    $dependencies = parent::calculateDependencies($field_definition);
    $entity_type_manager = \Drupal::entityTypeManager();
    $target_entity_type = $entity_type_manager->getDefinition($field_definition->getFieldStorageDefinition()
        ->getSetting('target_type'));
    // Depend on default values entity types configurations.
    if ($default_value = $field_definition->getDefaultValueLiteral()) {
        $entity_repository = \Drupal::service('entity.repository');
        foreach ($default_value as $value) {
            if (is_array($value) && isset($value['target_uuid'])) {
                $entity = $entity_repository->loadEntityByUuid($target_entity_type->id(), $value['target_uuid']);
                // If the entity does not exist do not create the dependency.
                // @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue()
                if ($entity) {
                    $dependencies[$target_entity_type->getConfigDependencyKey()][] = $entity->getConfigDependencyName();
                }
            }
        }
    }
    // Depend on target bundle configurations. Dependencies for 'target_bundles'
    // also covers the 'auto_create_bundle' setting, if any, because its value
    // is included in the 'target_bundles' list.
    $handler = $field_definition->getSetting('handler_settings');
    if (!empty($handler['target_bundles'])) {
        if ($bundle_entity_type_id = $target_entity_type->getBundleEntityType()) {
            if ($storage = $entity_type_manager->getStorage($bundle_entity_type_id)) {
                foreach ($storage->loadMultiple($handler['target_bundles']) as $bundle) {
                    $dependencies[$bundle->getConfigDependencyKey()][] = $bundle->getConfigDependencyName();
                }
            }
        }
    }
    return $dependencies;
}

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