function Importer::toEntity

Same name in other branches
  1. 11.x core/lib/Drupal/Core/DefaultContent/Importer.php \Drupal\Core\DefaultContent\Importer::toEntity()

Converts an array of content entity data to a content entity object.

Parameters

array<string, array<mixed>> $data: The entity data.

Return value

\Drupal\Core\Entity\ContentEntityInterface The unsaved entity.

Throws

\Drupal\Core\DefaultContent\ImportException If the `entity_type` or `uuid` meta keys are not set.

1 call to Importer::toEntity()
Importer::setFieldValues in core/lib/Drupal/Core/DefaultContent/Importer.php
Sets field values based on the normalized data.

File

core/lib/Drupal/Core/DefaultContent/Importer.php, line 180

Class

Importer
A service for handling import of content.

Namespace

Drupal\Core\DefaultContent

Code

private function toEntity(array $data) : ContentEntityInterface {
    if (empty($data['_meta']['entity_type'])) {
        throw new ImportException('The entity type metadata must be specified.');
    }
    if (empty($data['_meta']['uuid'])) {
        throw new ImportException('The uuid metadata must be specified.');
    }
    $is_root = FALSE;
    // @see ::loadEntityDependency()
    if ($this->dependencies === NULL && !empty($data['_meta']['depends'])) {
        $is_root = TRUE;
        foreach ($data['_meta']['depends'] as $uuid => $entity_type) {
            assert(is_string($uuid));
            assert(is_string($entity_type));
            $this->dependencies[$uuid] = [
                $entity_type,
                $uuid,
            ];
        }
    }
    [
        'entity_type' => $entity_type,
    ] = $data['_meta'];
    assert(is_string($entity_type));
    
    /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
    $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type);
    $values = [
        'uuid' => $data['_meta']['uuid'],
    ];
    if (!empty($data['_meta']['bundle'])) {
        $values[$entity_type->getKey('bundle')] = $data['_meta']['bundle'];
    }
    if (!empty($data['_meta']['default_langcode'])) {
        $data = $this->verifyNormalizedLanguage($data);
        $values[$entity_type->getKey('langcode')] = $data['_meta']['default_langcode'];
    }
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->entityTypeManager
        ->getStorage($entity_type->id())
        ->create($values);
    foreach ($data['default'] as $field_name => $values) {
        $this->setFieldValues($entity, $field_name, $values);
    }
    foreach ($data['translations'] ?? [] as $langcode => $translation_data) {
        if ($this->languageManager
            ->getLanguage($langcode)) {
            $translation = $entity->addTranslation($langcode, $entity->toArray());
            foreach ($translation_data as $field_name => $values) {
                $this->setFieldValues($translation, $field_name, $values);
            }
        }
    }
    if ($is_root) {
        $this->dependencies = NULL;
    }
    return $entity;
}

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