eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext::createDraft PHP Method

createDraft() public method

Uses a content type identifier + a hash of fields values to create and publish a draft below the root location.
public createDraft ( string $contentTypeIdentifier, array $fields ) : eZ\Publish\API\Repository\Values\Content\Content
$contentTypeIdentifier string
$fields array Hash of field def identifier => field value
return eZ\Publish\API\Repository\Values\Content\Content the created draft.
    public function createDraft($contentTypeIdentifier, array $fields)
    {
        $contentService = $this->repository->getContentService();
        $createStruct = $contentService->newContentCreateStruct($this->repository->getContentTypeService()->loadContentTypeByIdentifier($contentTypeIdentifier), 'eng-GB');
        foreach ($fields as $fieldDefIdentifier => $fieldValue) {
            $createStruct->setField($fieldDefIdentifier, $fieldValue);
        }
        $locationCreateStruct = $this->repository->getLocationService()->newLocationCreateStruct(2);
        $this->currentDraft = $this->repository->sudo(function () use($createStruct, $locationCreateStruct) {
            return $this->repository->getContentService()->createContent($createStruct, [$locationCreateStruct]);
        });
        return $this->currentDraft;
    }

Usage Example

 /**
  * @Given /^I create a draft for a content type that uses a custom location controller$/
  */
 public function iCreateDraftOfContentTypeWithCustomLocationController()
 {
     $this->contentContext->createDraft('blog_post', ['title' => 'Preview draft ' . date('c'), 'body' => '<?xml version="1.0" encoding="UTF-8"?><section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0"><para>This is a paragraph.</para></section>']);
 }