eZ\Publish\Core\REST\Server\Controller\Content::createRelation PHP Method

createRelation() public method

Creates a new relation of type COMMON for the given draft.
public createRelation ( mixed $contentId, integer $versionNumber, Request $request ) : CreatedRelation
$contentId mixed
$versionNumber integer
$request Symfony\Component\HttpFoundation\Request
return eZ\Publish\Core\REST\Server\Values\CreatedRelation
    public function createRelation($contentId, $versionNumber, Request $request)
    {
        $destinationContentId = $this->inputDispatcher->parse(new Message(array('Content-Type' => $request->headers->get('Content-Type')), $request->getContent()));
        $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
        $versionInfo = $this->repository->getContentService()->loadVersionInfo($contentInfo, $versionNumber);
        if ($versionInfo->status !== VersionInfo::STATUS_DRAFT) {
            throw new ForbiddenException('Relation of type COMMON can only be added to drafts');
        }
        try {
            $destinationContentInfo = $this->repository->getContentService()->loadContentInfo($destinationContentId);
        } catch (NotFoundException $e) {
            throw new ForbiddenException($e->getMessage());
        }
        $existingRelations = $this->repository->getContentService()->loadRelations($versionInfo);
        foreach ($existingRelations as $existingRelation) {
            if ($existingRelation->getDestinationContentInfo()->id == $destinationContentId) {
                throw new ForbiddenException('Relation of type COMMON to selected destination content ID already exists');
            }
        }
        $relation = $this->repository->getContentService()->addRelation($versionInfo, $destinationContentInfo);
        return new Values\CreatedRelation(array('relation' => new Values\RestRelation($relation, $contentId, $versionNumber)));
    }