function field_field_config_presave

field_field_config_presave(FieldConfigInterface $field)

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 388
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));
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!field!field.module/function/field_field_config_presave/8.1.x