You are here

protected function EntityReference::createEntity in Feeds 8.3

Creates a new entity with the given label and saves it.

Parameters

string $label: The label the new entity should get.

Return value

int|string|false The ID of the new entity or false if the given label is empty.

1 call to EntityReference::createEntity()
EntityReference::findEntities in src/Feeds/Target/EntityReference.php
Tries to lookup an existing entity.
1 method overrides EntityReference::createEntity()
File::createEntity in src/Feeds/Target/File.php
Creates a new entity with the given label and saves it.

File

src/Feeds/Target/EntityReference.php, line 336

Class

EntityReference
Defines an entity reference mapper.

Namespace

Drupal\feeds\Feeds\Target

Code

protected function createEntity($label) {
  if (!strlen(trim($label))) {
    return FALSE;
  }
  $bundles = $this
    ->getBundles();

  // Create values for the new entity.
  $values = [
    $this
      ->getLabelKey() => $label,
    $this
      ->getBundleKey() => reset($bundles),
  ];

  // Set language if the entity type supports it.
  if ($langcode = $this
    ->getLangcodeKey()) {
    $values[$langcode] = $this
      ->getLangcode();
  }
  $entity = $this->entityTypeManager
    ->getStorage($this
    ->getEntityType())
    ->create($values);
  $entity
    ->save();
  return $entity
    ->id();
}