You are here

public function ContentLoader::createEntity in YAML Content 8

Create the entity based on basic properties.

If existence checking is enabled, we'll attempt to load an existing entity matched on the simple properties before creating a new one.

Parameters

string $entity_type: The entity type.

array $content_data: The array of content data to be parsed.

Return value

\Drupal\Core\Entity\EntityInterface A loaded matching entity if existence checking is enabled and a matching entity was found, or a new one stubbed from simple properties otherwise.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

See also

\Drupal\yaml_content\ContentLoader\ContentLoader::existenceCheck()

1 call to ContentLoader::createEntity()
ContentLoader::buildEntity in src/ContentLoader/ContentLoader.php
Build an entity from the provided content data.

File

src/ContentLoader/ContentLoader.php, line 413

Class

ContentLoader
ContentLoader class for parsing and importing YAML content.

Namespace

Drupal\yaml_content\ContentLoader

Code

public function createEntity($entity_type, array $content_data) {

  // If existence checking is enabled, attempt to load the entity first.
  if ($this
    ->existenceCheck()) {
    $entity = $this
      ->entityExists($entity_type, $content_data);
  }

  // If the entity isn't loaded we'll stub it out.
  if (empty($entity)) {

    // Load entity type handler.
    $entity_handler = $this
      ->getEntityStorage($entity_type);

    // Identify the content properties for the entity.
    $attributes = $this
      ->getContentAttributes($entity_type, $content_data);
    $entity = $entity_handler
      ->create($attributes['property']);
  }
  return $entity;
}