You are here

public function DefaultSelection::createNewEntity in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::createNewEntity()
  2. 9 core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::createNewEntity()

Creates a new entity object that can be used as a valid reference.

Parameters

string $entity_type_id: The entity type ID.

string $bundle: The bundle name.

string $label: The entity label.

int $uid: The entity owner ID, if the entity type supports it.

Return value

\Drupal\Core\Entity\EntityInterface An unsaved entity object.

Overrides SelectionWithAutocreateInterface::createNewEntity

1 method overrides DefaultSelection::createNewEntity()
NodeSelection::createNewEntity in core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php
Creates a new entity object that can be used as a valid reference.

File

core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php, line 391

Class

DefaultSelection
Default plugin implementation of the Entity Reference Selection plugin.

Namespace

Drupal\Core\Entity\Plugin\EntityReferenceSelection

Code

public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
  $entity_type = $this->entityTypeManager
    ->getDefinition($entity_type_id);
  $values = [
    $entity_type
      ->getKey('label') => $label,
  ];
  if ($bundle_key = $entity_type
    ->getKey('bundle')) {
    $values[$bundle_key] = $bundle;
  }
  $entity = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->create($values);
  if ($entity instanceof EntityOwnerInterface) {
    $entity
      ->setOwnerId($uid);
  }
  return $entity;
}