You are here

public function ContentLoader::buildEntity in YAML Content 8

Build an entity from the provided content data.

Parameters

string $entity_type: The entity type.

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

Return value

\Drupal\Core\Entity\EntityInterface The created entity from the parsed content data.

Throws

\Drupal\Core\Entity\EntityStorageException

Overrides ContentLoaderInterface::buildEntity

2 calls to ContentLoader::buildEntity()
ContentLoader::loadContent in src/ContentLoader/ContentLoader.php
Load all demo content for this loader.
ContentLoader::populateField in src/ContentLoader/ContentLoader.php
Populate field content into the provided field.

File

src/ContentLoader/ContentLoader.php, line 345

Class

ContentLoader
ContentLoader class for parsing and importing YAML content.

Namespace

Drupal\yaml_content\ContentLoader

Code

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

  // Load entity type definition.
  $entity_definition = $this
    ->getEntityTypeDefinition($entity_type);

  // Dispatch the entity import event.
  $entity_import_event = new EntityImportEvent($this, $entity_definition, $content_data);
  $this
    ->getEventDispatcher()
    ->dispatch(YamlContentEvents::IMPORT_ENTITY, $entity_import_event);

  // Parse properties for creation and fields for processing.
  $attributes = $this
    ->getContentAttributes($entity_type, $content_data);

  // If it is a 'user' entity, append a timestamp to make the username unique.
  // @todo Move this into an entity-specific processor.
  if ($entity_type == 'user' && isset($attributes['property']['name'][0]['value'])) {
    $attributes['property']['name'][0]['value'] .= '_' . time();
  }

  // Create our entity with basic data.
  $entity = $this
    ->createEntity($entity_type, $content_data);

  // Populate fields.
  if ($entity instanceof FieldableEntityInterface) {
    $this
      ->populateEntityFields($entity, $attributes['field']);
  }
  return $entity;
}