function FieldHooks::fieldConfigCreate

Implements hook_ENTITY_TYPE_create() for 'field_config'.

Determine the selection handler plugin ID for an entity reference field.

File

core/modules/field/src/Hook/FieldHooks.php, line 344

Class

FieldHooks
Hook implementations for field.

Namespace

Drupal\field\Hook

Code

public function fieldConfigCreate(FieldConfigInterface $field) {
    if ($field->isSyncing()) {
        return;
    }
    // Act on all sub-types of the entity_reference field type.
    
    /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    $item_class = 'Drupal\\Core\\Field\\Plugin\\Field\\FieldType\\EntityReferenceItem';
    $class = $field_type_manager->getPluginClass($field->getType());
    if ($class !== $item_class && !is_subclass_of($class, $item_class)) {
        return;
    }
    // If we don't know the target type yet, there's nothing else we can do.
    $target_type = $field->getFieldStorageDefinition()
        ->getSetting('target_type');
    if (empty($target_type)) {
        return;
    }
    // Make sure the selection handler plugin is the correct derivative for the
    // target entity type.
    $selection_manager = \Drupal::service('plugin.manager.entity_reference_selection');
    [
        $current_handler,
    ] = explode(':', $field->getSetting('handler'), 2);
    $field->setSetting('handler', $selection_manager->getPluginId($target_type, $current_handler));
}

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