Implements hook_field_presave().
Create any new terms defined in a freetagging vocabulary.
function taxonomy_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => $item) {
if (!$item['tid'] && isset($item['entity'])) {
unset($item['tid']);
$item['entity']
->save();
$items[$delta]['tid'] = $item['entity']
->id();
}
}
}