function FieldHooks::fieldConfigPresave

Implements hook_ENTITY_TYPE_presave() for 'field_config'.

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

File

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

Class

FieldHooks
Hook implementations for field.

Namespace

Drupal\field\Hook

Code

public function fieldConfigPresave(FieldConfigInterface $field) {
    // Don't change anything during a configuration sync.
    if ($field->isSyncing()) {
        return;
    }
    $this->fieldConfigCreate($field);
    // 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;
    }
    // In case we removed all the target bundles allowed by the field in
    // EntityReferenceItem::onDependencyRemoval() or field_entity_bundle_delete()
    // we have to log a critical message because the field will not function
    // correctly anymore.
    $handler_settings = $field->getSetting('handler_settings');
    if (isset($handler_settings['target_bundles']) && $handler_settings['target_bundles'] === []) {
        \Drupal::logger('entity_reference')->critical('The %field_name entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', [
            '%field_name' => $field->getName(),
            '%entity_type' => $field->getTargetEntityTypeId(),
            '%bundle' => $field->getTargetBundle(),
        ]);
    }
}

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