eZ\Publish\Core\Repository\LocationService::createLocation PHP Method

createLocation() public method

Creates the new $location in the content repository for the given content.
public createLocation ( eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo, eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct ) : eZ\Publish\API\Repository\Values\Content\Location
$contentInfo eZ\Publish\API\Repository\Values\Content\ContentInfo
$locationCreateStruct eZ\Publish\API\Repository\Values\Content\LocationCreateStruct
return eZ\Publish\API\Repository\Values\Content\Location the newly created Location
    public function createLocation(ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct)
    {
        $content = $this->repository->getContentService()->loadContent($contentInfo->id);
        $parentLocation = $this->loadLocation($locationCreateStruct->parentLocationId);
        if (!$this->repository->canUser('content', 'create', $content->contentInfo, $parentLocation)) {
            throw new UnauthorizedException('content', 'create');
        }
        // Check if the parent is a sub location of one of the existing content locations (this also solves the
        // situation where parent location actually one of the content locations),
        // or if the content already has location below given location create struct parent
        $existingContentLocations = $this->loadLocations($content->contentInfo);
        if (!empty($existingContentLocations)) {
            foreach ($existingContentLocations as $existingContentLocation) {
                if (stripos($parentLocation->pathString, $existingContentLocation->pathString) !== false) {
                    throw new InvalidArgumentException('$locationCreateStruct', 'Specified parent is a sub location of one of the existing content locations.');
                }
                if ($parentLocation->id == $existingContentLocation->parentLocationId) {
                    throw new InvalidArgumentException('$locationCreateStruct', 'Content is already below the specified parent.');
                }
            }
        }
        $spiLocationCreateStruct = $this->domainMapper->buildSPILocationCreateStruct($locationCreateStruct, $parentLocation, $content->contentInfo->mainLocationId !== null ? $content->contentInfo->mainLocationId : true, $content->contentInfo->id, $content->contentInfo->currentVersionNo);
        $this->repository->beginTransaction();
        try {
            $newLocation = $this->persistenceHandler->locationHandler()->create($spiLocationCreateStruct);
            $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content);
            foreach ($urlAliasNames as $languageCode => $name) {
                $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation($newLocation->id, $newLocation->parentId, $name, $languageCode, $content->contentInfo->alwaysAvailable, $languageCode === $content->contentInfo->mainLanguageCode);
            }
            $this->repository->commit();
        } catch (Exception $e) {
            $this->repository->rollback();
            throw $e;
        }
        return $this->domainMapper->buildLocationDomainObject($newLocation);
    }