function Importer::importContent

Same name and namespace in other branches
  1. 10 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.

\Drupal\Core\Session\AccountInterface|null $account: (optional) The account to use when importing the entities. Defaults to the administrator account.

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 74

Class

Importer
A service for handling import of content.

Namespace

Drupal\Core\DefaultContent

Code

public function importContent(Finder $content, Existing $existing = Existing::Error, ?AccountInterface $account = NULL) : void {
  if (count($content->data) === 0) {
    return;
  }
  $event = new PreImportEvent($content, $existing);
  $skip = $this->eventDispatcher
    ->dispatch($event)
    ->getSkipList();
  if ($account !== NULL) {
    $this->accountSwitcher
      ->switchTo($account);
  }
  else {
    $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));
      // The event subscribers asked to skip importing this entity. If they
      // explained why, log that.
      if (array_key_exists($uuid, $skip)) {
        if ($skip[$uuid]) {
          $this->logger?->info('Skipped importing @entity_type @uuid because: %reason', [
            '@entity_type' => $entity_type_id,
            '@uuid' => $uuid,
            '%reason' => $skip[$uuid],
          ]);
        }
        continue;
      }
      $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.