You are here

function field_field_config_presave in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/field.module \field_field_config_presave()

Implements hook_ENTITY_TYPE_presave() for 'field_config'.

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

File

core/modules/field/field.module, line 385
Attach custom data fields to Drupal entities.

Code

function field_field_config_presave(FieldConfigInterface $field) {

  // Don't change anything during a configuration sync.
  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;
  }

  // Make sure the selection handler plugin is the correct derivative for the
  // target entity type.
  $target_type = $field
    ->getFieldStorageDefinition()
    ->getSetting('target_type');
  $selection_manager = \Drupal::service('plugin.manager.entity_reference_selection');
  list($current_handler) = explode(':', $field
    ->getSetting('handler'), 2);
  $field
    ->setSetting('handler', $selection_manager
    ->getPluginId($target_type, $current_handler));
}