You are here

public function DefaultSelection::createNewEntity in Drupal 8

Same name and namespace in other branches
  1. 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

6 calls to DefaultSelection::createNewEntity()
CommentSelection::createNewEntity in core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php
Creates a new entity object that can be used as a valid reference.
FileSelection::createNewEntity in core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php
Creates a new entity object that can be used as a valid reference.
MediaSelection::createNewEntity in core/modules/media/src/Plugin/EntityReferenceSelection/MediaSelection.php
Creates a new entity object that can be used as a valid reference.
NodeSelection::createNewEntity in core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php
Creates a new entity object that can be used as a valid reference.
TermSelection::createNewEntity in core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php
Creates a new entity object that can be used as a valid reference.

... See full list

6 methods override DefaultSelection::createNewEntity()
CommentSelection::createNewEntity in core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php
Creates a new entity object that can be used as a valid reference.
FileSelection::createNewEntity in core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php
Creates a new entity object that can be used as a valid reference.
MediaSelection::createNewEntity in core/modules/media/src/Plugin/EntityReferenceSelection/MediaSelection.php
Creates a new entity object that can be used as a valid reference.
NodeSelection::createNewEntity in core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php
Creates a new entity object that can be used as a valid reference.
TermSelection::createNewEntity in core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php
Creates a new entity object that can be used as a valid reference.

... See full list

File

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

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