Creates a new entity from a label entered in the autocomplete input.
string $label: The entity label.
int $uid: The entity uid.
\Drupal\Core\Entity\EntityInterface
protected function createNewEntity($label, $uid) {
$entity_manager = \Drupal::entityManager();
$target_type = $this->field['settings']['target_type'];
// Get the bundle.
if (!empty($this->instance['settings']['handler_settings']['target_bundles']) && count($this->instance['settings']['handler_settings']['target_bundles']) == 1) {
$bundle = reset($this->instance['settings']['handler_settings']['target_bundles']);
}
else {
$bundles = entity_get_bundles($target_type);
$bundle = reset($bundles);
}
$entity_info = $entity_manager
->getDefinition($target_type);
$bundle_key = $entity_info['entity_keys']['bundle'];
$label_key = $entity_info['entity_keys']['label'];
return $entity_manager
->getStorageController($target_type)
->create(array(
$label_key => $label,
$bundle_key => $bundle,
'uid' => $uid,
));
}