protected function AutocompleteWidgetBase::createNewEntity

Creates a new entity from a label entered in the autocomplete input.

Parameters

string $label: The entity label.

int $uid: The entity uid.

Return value

\Drupal\Core\Entity\EntityInterface

2 calls to AutocompleteWidgetBase::createNewEntity()
AutocompleteTagsWidget::elementValidate in drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php
Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::elementValidate()
AutocompleteWidget::elementValidate in drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php
Overrides \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase::elementValidate()

File

drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php, line 138
Contains \Drupal\entity_reference\Plugin\field\widget\AutocompleteWidgetBase.

Class

AutocompleteWidgetBase
Parent plugin for entity reference autocomplete widgets.

Namespace

Drupal\entity_reference\Plugin\field\widget

Code

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