Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

BasicContentContext.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\Features\Context;
8
9 use Behat\Behat\Context\Context;
10 use EzSystems\PlatformBehatBundle\Context\RepositoryContext;
11 use eZ\Publish\API\Repository\ContentTypeService;
12 use eZ\Publish\API\Repository\ContentService;
13 use eZ\Publish\API\Repository\Repository;
14
18 class BasicContentContext implements Context
19 {
20 use RepositoryContext;
21
25 const DEFAULT_LANGUAGE = 'eng-GB';
26
30 private $contentPaths = [];
31
33 private $contentTypeService;
34
36 private $contentService;
37
43 public function __construct(
44 Repository $repository,
45 ContentTypeService $contentTypeService,
46 ContentService $contentService
47 ) {
48 $this->setRepository($repository);
49 $this->contentTypeService = $contentTypeService;
50 $this->contentService = $contentService;
51 }
52
62 public function createContent($contentType, $fields, $parentLocationId)
63 {
64 $repository = $this->getRepository();
65 $languageCode = self::DEFAULT_LANGUAGE;
66 $content = $this->createContentDraft($parentLocationId, $contentType, $fields, $languageCode);
67 $content = $this->contentService->publishVersion($content->versionInfo);
68
69 return $content->contentInfo->mainLocationId;
70 }
71
77 public function publishDraft(Content $content)
78 {
79 $this->contentService->publishVersion($content->versionInfo->id);
80 }
81
92 public function createContentDraft($parentLocationId, $contentTypeIdentifier, $fields, $languageCode = null)
93 {
94 $languageCode = $languageCode ?: self::DEFAULT_LANGUAGE;
95 $repository = $this->getRepository();
96 $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct($parentLocationId);
97 $contentTypeIdentifier = $this->contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
98 $contentCreateStruct = $this->contentService->newContentCreateStruct($contentTypeIdentifier, $languageCode);
99 foreach (array_keys($fields) as $key) {
100 $contentCreateStruct->setField($key, $fields[$key]);
101 }
102
103 return $this->contentService->createContent($contentCreateStruct, [$locationCreateStruct]);
104 }
105
116 public function createContentWithPath($path, $fields, $contentType)
117 {
118 $contentsName = explode('/', $path);
119 $currentPath = '';
120 $location = '2';
121 foreach ($contentsName as $name) {
122 if ($name != end($contentsName)) {
123 $location = $this->createContent('folder', ['name' => $name], $location);
124 }
125 if ($currentPath != '') {
126 $currentPath .= '/';
127 }
128 $currentPath .= $name;
129 $this->mapContentPath($currentPath);
130 }
131 $location = $this->createContent($contentType, $fields, $location);
132
133 return $location;
134 }
135
139 public function getContentPath($name)
140 {
141 return $this->contentPaths[$name];
142 }
143
147 private function mapContentPath($path)
148 {
149 $contentNames = explode('/', $path);
150 $this->contentPaths[end($contentNames)] = $path;
151 }
152
156 public function createBasicFolder($path)
157 {
158 $fields = ['name' => $this->getTitleFromPath($path)];
159
160 return $this->createContentwithPath($path, $fields, 'folder');
161 }
162
166 public function createBasicArticle($path)
167 {
168 $fields = [
169 'title' => $this->getTitleFromPath($path),
170 'intro' => $this->getDummyXmlText(),
171 ];
172
173 return $this->createContentwithPath($path, $fields, 'article');
174 }
175
179 public function createArticleDraft($path)
180 {
181 $fields = [
182 'title' => $this->getTitleFromPath($path),
183 'intro' => $this->getDummyXmlText(),
184 ];
185
186 return $this->createContentDraft(2, 'article', $fields);
187 }
188
189 private function getTitleFromPath($path)
190 {
191 $parts = explode('/', rtrim($path, '/'));
192
193 return end($parts);
194 }
195
199 private function getDummyXmlText()
200 {
201 return '<?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>';
202 }
203 }
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext
Sentences for simple Contents creation.
Definition: BasicContentContext.php:19
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\$contentTypeService
$contentTypeService
Definition: BasicContentContext.php:33
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\getContentPath
getContentPath($name)
Getter for contentPaths.
Definition: BasicContentContext.php:139
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\$contentPaths
$contentPaths
Content path mapping.
Definition: BasicContentContext.php:30
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\publishDraft
publishDraft(Content $content)
Publishes a content draft.
Definition: BasicContentContext.php:77
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\__construct
__construct(Repository $repository, ContentTypeService $contentTypeService, ContentService $contentService)
@injectService $repository @ezpublish.api.repository @injectService $contentTypeService @ezpublish....
Definition: BasicContentContext.php:43
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\getDummyXmlText
getDummyXmlText()
Definition: BasicContentContext.php:199
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\createContent
createContent($contentType, $fields, $parentLocationId)
Creates and publishes a Content.
Definition: BasicContentContext.php:62
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\createContentDraft
createContentDraft($parentLocationId, $contentTypeIdentifier, $fields, $languageCode=null)
Creates a content draft.
Definition: BasicContentContext.php:92
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\createContentWithPath
createContentWithPath($path, $fields, $contentType)
Creates and publishes a content at a given path.
Definition: BasicContentContext.php:116
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\DEFAULT_LANGUAGE
const DEFAULT_LANGUAGE
Default language.
Definition: BasicContentContext.php:25
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\createBasicArticle
createBasicArticle($path)
@Given a/an :path article exists
Definition: BasicContentContext.php:166
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\$contentService
$contentService
Definition: BasicContentContext.php:36
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\mapContentPath
mapContentPath($path)
Maps the path of the content to it's name for later use.
Definition: BasicContentContext.php:147
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\createArticleDraft
createArticleDraft($path)
@Given a/an :path article draft exists
Definition: BasicContentContext.php:179
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\getTitleFromPath
getTitleFromPath($path)
Definition: BasicContentContext.php:189
eZ\Bundle\EzPublishCoreBundle\Features\Context\BasicContentContext\createBasicFolder
createBasicFolder($path)
@Given a/an :path folder exists
Definition: BasicContentContext.php:156
eZ\Publish\API\Repository\Values\Content\Content
Definition: Publish/API/Repository/Values/Content/Content.php:21
eZ\Publish\API\Repository\ContentService
Definition: API/Repository/ContentService.php:27
eZ\Publish\API\Repository\ContentTypeService
Definition: API/Repository/ContentTypeService.php:25
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Bundle\EzPublishCoreBundle\Features\Context
Definition: BasicContentContext.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.