function Importer::importContent

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

Imports content entities from disk.

Parameters

\Drupal\Core\DefaultContent\Finder $content: The content finder, which has information on the entities to create in the necessary dependency order.

\Drupal\Core\DefaultContent\Existing $existing: (optional) What to do if one of the entities being imported already exists, by UUID:

  • \Drupal\Core\DefaultContent\Existing::Error: Throw an exception.
  • \Drupal\Core\DefaultContent\Existing::Skip: Leave the existing entity as-is.

Throws

\Drupal\Core\DefaultContent\ImportException

  • If any of the entities being imported are not content entities.
  • If any of the entities being imported already exists, by UUID, and $existing is \Drupal\Core\DefaultContent\Existing::Error.

File

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

Class

Importer
A service for handling import of content.

Namespace

Drupal\Core\DefaultContent

Code

public function importContent(Finder $content, Existing $existing = Existing::Error) : void {
    if (count($content->data) === 0) {
        return;
    }
    $account = $this->accountSwitcher
        ->switchToAdministrator();
    try {
        
        /** @var array{_meta: array<mixed>} $decoded */
        foreach ($content->data as $decoded) {
            [
                'uuid' => $uuid,
                'entity_type' => $entity_type_id,
                'path' => $path,
            ] = $decoded['_meta'];
            assert(is_string($uuid));
            assert(is_string($entity_type_id));
            assert(is_string($path));
            $entity_type = $this->entityTypeManager
                ->getDefinition($entity_type_id);
            
            /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
            if (!$entity_type->entityClassImplements(ContentEntityInterface::class)) {
                throw new ImportException("Content entity {$uuid} is a '{$entity_type_id}', which is not a content entity type.");
            }
            $entity = $this->entityRepository
                ->loadEntityByUuid($entity_type_id, $uuid);
            if ($entity) {
                if ($existing === Existing::Skip) {
                    continue;
                }
                else {
                    throw new ImportException("{$entity_type_id} {$uuid} already exists.");
                }
            }
            $entity = $this->toEntity($decoded)
                ->enforceIsNew();
            // Ensure that the entity is not owned by the anonymous user.
            if ($entity instanceof EntityOwnerInterface && empty($entity->getOwnerId())) {
                $entity->setOwnerId($account->id());
            }
            // If a file exists in the same folder, copy it to the designated
            // target URI.
            if ($entity instanceof FileInterface) {
                $this->copyFileAssociatedWithEntity(dirname($path), $entity);
            }
            $violations = $entity->validate();
            if (count($violations) > 0) {
                throw new InvalidEntityException($violations, $path);
            }
            $entity->save();
        }
    } finally {
        $this->accountSwitcher
            ->switchBack();
    }
}

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