Neos\ContentRepository\Domain\Service\ImportExport\NodeImportService::import PHP Method

import() public method

The root node of the imported tree becomes a child of the node specified as the target path, as the following example illustrates: 1. Existing Nodes Before Import: path - to - - my - - - targetNode - - - - A - other - - nodes 2. Sub-tree in xml to import to 'path/to/my/targetNode': - 3. existing nodes after the import: path - to - - my - - - targetNode - - - - A - - - - B - - - - - B1 - another - - sub-tree
public import ( XMLReader $xmlReader, string $targetPath, string $resourceLoadPath = null ) : void
$xmlReader XMLReader The XML input to import - must be either XML as a string or a prepared \XMLReader instance containing XML data
$targetPath string path to the node which becomes parent of the root of the imported sub-tree
$resourceLoadPath string
return void
    public function import(\XMLReader $xmlReader, $targetPath, $resourceLoadPath = null)
    {
        $this->propertyMappingConfiguration = new ImportExportPropertyMappingConfiguration($resourceLoadPath);
        $this->nodeNameStack = $targetPath === '/' ? array() : array_map(function ($pathSegment) {
            return Utility::renderValidNodeName($pathSegment);
        }, explode('/', $targetPath));
        $formatVersion = $this->determineFormatVersion($xmlReader);
        switch ($formatVersion) {
            case self::SUPPORTED_FORMAT_VERSION:
                $this->securityContext->withoutAuthorizationChecks(function () use($xmlReader) {
                    $this->importSubtree($xmlReader);
                });
                break;
            case null:
                throw new ImportException('Failed to recognize format of the Node Data XML to import. Please make sure that you use a valid Node Data XML structure.', 1409059346);
            default:
                throw new ImportException('Failed to import Node Data XML: The format with version ' . $formatVersion . ' is not supported, only version ' . self::SUPPORTED_FORMAT_VERSION . ' is supported.', 1409059352);
        }
    }

Usage Example

 /**
  * Imports one or multiple sites from the XML file at $pathAndFilename
  *
  * @param string $pathAndFilename
  * @return Site The imported site
  * @throws UnknownPackageException|InvalidPackageStateException|NeosException
  */
 public function importFromFile($pathAndFilename)
 {
     /** @var Site $importedSite */
     $site = null;
     $xmlReader = new \XMLReader();
     $xmlReader->open($pathAndFilename, null, LIBXML_PARSEHUGE);
     if ($this->workspaceRepository->findOneByName('live') === null) {
         $this->workspaceRepository->add(new Workspace('live'));
         $this->persistenceManager->persistAll();
     }
     while ($xmlReader->read()) {
         if ($xmlReader->nodeType != \XMLReader::ELEMENT || $xmlReader->name !== 'site') {
             continue;
         }
         $site = $this->getSiteByNodeName($xmlReader->getAttribute('siteNodeName'));
         $site->setName($xmlReader->getAttribute('name'));
         $site->setState((int) $xmlReader->getAttribute('state'));
         $siteResourcesPackageKey = $xmlReader->getAttribute('siteResourcesPackageKey');
         if (!$this->packageManager->isPackageAvailable($siteResourcesPackageKey)) {
             throw new UnknownPackageException(sprintf('Package "%s" specified in the XML as site resources package does not exist.', $siteResourcesPackageKey), 1303891443);
         }
         if (!$this->packageManager->isPackageActive($siteResourcesPackageKey)) {
             throw new InvalidPackageStateException(sprintf('Package "%s" specified in the XML as site resources package is not active.', $siteResourcesPackageKey), 1303898135);
         }
         $site->setSiteResourcesPackageKey($siteResourcesPackageKey);
         $rootNode = $this->contextFactory->create()->getRootNode();
         // We fetch the workspace to be sure it's known to the persistence manager and persist all
         // so the workspace and site node are persisted before we import any nodes to it.
         $rootNode->getContext()->getWorkspace();
         $this->persistenceManager->persistAll();
         $sitesNode = $rootNode->getNode(SiteService::SITES_ROOT_PATH);
         if ($sitesNode === null) {
             $sitesNode = $rootNode->createNode(NodePaths::getNodeNameFromPath(SiteService::SITES_ROOT_PATH));
         }
         $this->nodeImportService->import($xmlReader, $sitesNode->getPath(), dirname($pathAndFilename) . '/Resources');
     }
     if ($site === null) {
         throw new NeosException(sprintf('The XML file did not contain a valid site node.'), 1418999522);
     }
     $this->emitSiteImported($site);
     return $site;
 }
All Usage Examples Of Neos\ContentRepository\Domain\Service\ImportExport\NodeImportService::import