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

createContentDraft() public method

If no version is given, the current published version is used. 4.x: The draft is created with the initialLanguage code of the source version or if not present with the main language. It can be changed on updating the version.
public createContentDraft ( eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo, eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo = null, eZ\Publish\API\Repository\Values\User\User $creator = null ) : eZ\Publish\API\Repository\Values\Content\Content
$contentInfo eZ\Publish\API\Repository\Values\Content\ContentInfo
$versionInfo eZ\Publish\API\Repository\Values\Content\VersionInfo
$creator eZ\Publish\API\Repository\Values\User\User if set given user is used to create the draft - otherwise the current-user is used
return eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft
    public function createContentDraft(ContentInfo $contentInfo, APIVersionInfo $versionInfo = null, User $creator = null)
    {
        $contentInfo = $this->loadContentInfo($contentInfo->id);
        if ($versionInfo !== null) {
            // Check that given $contentInfo and $versionInfo belong to the same content
            if ($versionInfo->getContentInfo()->id != $contentInfo->id) {
                throw new InvalidArgumentException('$versionInfo', 'VersionInfo does not belong to the same content as given ContentInfo');
            }
            $versionInfo = $this->loadVersionInfoById($contentInfo->id, $versionInfo->versionNo);
            switch ($versionInfo->status) {
                case VersionInfo::STATUS_PUBLISHED:
                case VersionInfo::STATUS_ARCHIVED:
                    break;
                default:
                    // @todo: throw an exception here, to be defined
                    throw new BadStateException('$versionInfo', 'Draft can not be created from a draft version');
            }
            $versionNo = $versionInfo->versionNo;
        } elseif ($contentInfo->published) {
            $versionNo = $contentInfo->currentVersionNo;
        } else {
            // @todo: throw an exception here, to be defined
            throw new BadStateException('$contentInfo', 'Content is not published, draft can be created only from published or archived version');
        }
        if ($creator === null) {
            $creator = $this->repository->getCurrentUserReference();
        }
        if (!$this->repository->canUser('content', 'edit', $contentInfo)) {
            throw new UnauthorizedException('content', 'edit', array('contentId' => $contentInfo->id));
        }
        $this->repository->beginTransaction();
        try {
            $spiContent = $this->persistenceHandler->contentHandler()->createDraftFromVersion($contentInfo->id, $versionNo, $creator->getUserId());
            $this->repository->commit();
        } catch (Exception $e) {
            $this->repository->rollback();
            throw $e;
        }
        return $this->domainMapper->buildContentDomainObject($spiContent);
    }