function entity_reference_field_presave

Implements hook_field_presave().

Create an entity on the fly.

File

drupal/core/modules/entity_reference/entity_reference.module, line 108
Provides a field that can reference other entities.

Code

function entity_reference_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) {
  foreach ($items as $delta => $item) {
    if (empty($item['target_id']) && !empty($item['entity']) && $item['entity']
      ->isNew()) {
      $item['entity']
        ->save();
      $items[$delta]['target_id'] = $item['entity']
        ->id();
    }
  }
}