eZ\Publish\Core\Repository\ContentService::copyContent PHP Method

copyContent() public method

Copies the content to a new location. If no version is given, all versions are copied, otherwise only the given version.
public copyContent ( eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo, eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $destinationLocationCreateStruct, eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo = null ) : eZ\Publish\API\Repository\Values\Content\Content
$contentInfo eZ\Publish\API\Repository\Values\Content\ContentInfo
$destinationLocationCreateStruct eZ\Publish\API\Repository\Values\Content\LocationCreateStruct the target location where the content is copied to
$versionInfo eZ\Publish\API\Repository\Values\Content\VersionInfo
return eZ\Publish\API\Repository\Values\Content\Content
    public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, APIVersionInfo $versionInfo = null)
    {
        if (!$this->repository->canUser('content', 'create', $contentInfo, $destinationLocationCreateStruct)) {
            throw new UnauthorizedException('content', 'create', array('parentLocationId' => $destinationLocationCreateStruct->parentLocationId, 'sectionId' => $contentInfo->sectionId));
        }
        $defaultObjectStates = $this->getDefaultObjectStates();
        $this->repository->beginTransaction();
        try {
            $spiContent = $this->persistenceHandler->contentHandler()->copy($contentInfo->id, $versionInfo ? $versionInfo->versionNo : null);
            foreach ($defaultObjectStates as $objectStateGroupId => $objectState) {
                $this->persistenceHandler->objectStateHandler()->setContentState($spiContent->versionInfo->contentInfo->id, $objectStateGroupId, $objectState->id);
            }
            $content = $this->internalPublishVersion($this->domainMapper->buildVersionInfoDomainObject($spiContent->versionInfo), $spiContent->versionInfo->creationDate);
            $this->repository->getLocationService()->createLocation($content->getVersionInfo()->getContentInfo(), $destinationLocationCreateStruct);
            $this->repository->commit();
        } catch (Exception $e) {
            $this->repository->rollback();
            throw $e;
        }
        return $this->internalLoadContent($content->id);
    }