function EntityReferenceItem::onDependencyRemoval

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

Overrides FieldItemBase::onDependencyRemoval

File

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

Class

EntityReferenceItem
Defines the 'entity_reference' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) {
    $changed = parent::onDependencyRemoval($field_definition, $dependencies);
    $entity_type_manager = \Drupal::entityTypeManager();
    $target_entity_type = $entity_type_manager->getDefinition($field_definition->getFieldStorageDefinition()
        ->getSetting('target_type'));
    // Try to update the default value config dependency, if possible.
    if ($default_value = $field_definition->getDefaultValueLiteral()) {
        $entity_repository = \Drupal::service('entity.repository');
        foreach ($default_value as $key => $value) {
            if (is_array($value) && isset($value['target_uuid'])) {
                $entity = $entity_repository->loadEntityByUuid($target_entity_type->id(), $value['target_uuid']);
                // @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue()
                if ($entity && isset($dependencies[$entity->getConfigDependencyKey()][$entity->getConfigDependencyName()])) {
                    unset($default_value[$key]);
                    $changed = TRUE;
                }
            }
        }
        if ($changed) {
            $field_definition->setDefaultValue($default_value);
        }
    }
    // Update the 'target_bundles' handler setting if a bundle config dependency
    // has been removed.
    $bundles_changed = FALSE;
    $handler_settings = $field_definition->getSetting('handler_settings');
    if (!empty($handler_settings['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_settings['target_bundles']) as $bundle) {
                    if (isset($dependencies[$bundle->getConfigDependencyKey()][$bundle->getConfigDependencyName()])) {
                        unset($handler_settings['target_bundles'][$bundle->id()]);
                        // If this bundle is also used in the 'auto_create_bundle'
                        // setting, disable the auto-creation feature completely.
                        $auto_create_bundle = !empty($handler_settings['auto_create_bundle']) ? $handler_settings['auto_create_bundle'] : FALSE;
                        if ($auto_create_bundle && $auto_create_bundle == $bundle->id()) {
                            $handler_settings['auto_create'] = FALSE;
                            $handler_settings['auto_create_bundle'] = NULL;
                        }
                        $bundles_changed = TRUE;
                    }
                }
            }
        }
    }
    if ($bundles_changed) {
        $field_definition->setSetting('handler_settings', $handler_settings);
    }
    $changed |= $bundles_changed;
    return $changed;
}

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