function DefaultSelection::createNewEntity

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::createNewEntity()
  2. 10 core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::createNewEntity()
2 methods override DefaultSelection::createNewEntity()
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.

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.