You are here

function dynamic_entity_reference_field_config_presave in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 dynamic_entity_reference.module \dynamic_entity_reference_field_config_presave()

Implements hook_ENTITY_TYPE_presave() for 'field_config'.

@todo Clean this up once field_field_config_save is more accommodating.

File

./dynamic_entity_reference.module, line 68
Contains main module functionality.

Code

function dynamic_entity_reference_field_config_presave(FieldConfigInterface $field) {

  // Don't change anything during a configuration sync.
  if ($field
    ->isSyncing()) {
    return;
  }
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  $er_item_class = EntityReferenceItem::class;
  $class = $field_type_manager
    ->getPluginClass($field
    ->getType());
  if ($class !== $er_item_class && !is_subclass_of($class, $er_item_class)) {
    return;
  }
  $der_item_class = DynamicEntityReferenceItem::class;
  if ($class === $der_item_class || is_subclass_of($class, $der_item_class)) {

    // DER needs to be handled differently.
    return;
  }

  // Just a normal ER item, do the things field_field_config_presave() would
  // have done before we removed it in
  // dynamic_entity_reference_module_implements_alter().
  $selection_manager = \Drupal::service('plugin.manager.entity_reference_selection');
  $target_type = $field
    ->getFieldStorageDefinition()
    ->getSetting('target_type');
  list($current_handler) = explode(':', $field
    ->getSetting('handler'), 2);
  $field
    ->setSetting('handler', $selection_manager
    ->getPluginId($target_type, $current_handler));
}