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

ContentViewBuilder.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\MVC\Symfony\View\Builder;
8
9 use eZ\Publish\API\Repository\Exceptions\NotFoundException;
10 use eZ\Publish\API\Repository\Repository;
11 use eZ\Publish\API\Repository\Values\Content\Content;
12 use eZ\Publish\API\Repository\Values\Content\Location;
13 use eZ\Publish\API\Repository\Values\Content\VersionInfo;
14 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
15 use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
16 use eZ\Publish\Core\Helper\ContentInfoLocationLoader;
17 use eZ\Publish\Core\MVC\Exception\HiddenLocationException;
18 use eZ\Publish\Core\MVC\Symfony\Controller\Content\PreviewController;
19 use eZ\Publish\Core\MVC\Symfony\View\Configurator;
20 use eZ\Publish\Core\MVC\Symfony\View\ContentView;
21 use eZ\Publish\Core\MVC\Symfony\View\EmbedView;
22 use eZ\Publish\Core\MVC\Symfony\View\ParametersInjector;
23 use Symfony\Component\HttpFoundation\RequestStack;
24 use Symfony\Component\HttpKernel\Controller\ControllerReference;
25
29 class ContentViewBuilder implements ViewBuilder
30 {
32 private $repository;
33
35 private $permissionResolver;
36
38 private $viewConfigurator;
39
41 private $viewParametersInjector;
42
44 private $requestStack;
45
51 private $defaultTemplates;
52
54 private $locationLoader;
55
56 public function __construct(
57 Repository $repository,
58 Configurator $viewConfigurator,
59 ParametersInjector $viewParametersInjector,
60 RequestStack $requestStack,
61 ContentInfoLocationLoader $locationLoader = null
62 ) {
63 $this->repository = $repository;
64 $this->viewConfigurator = $viewConfigurator;
65 $this->viewParametersInjector = $viewParametersInjector;
66 $this->locationLoader = $locationLoader;
67 $this->permissionResolver = $this->repository->getPermissionResolver();
68 $this->requestStack = $requestStack;
69 }
70
71 public function matches($argument)
72 {
73 return strpos($argument, 'ez_content:') !== false;
74 }
75
86 public function buildView(array $parameters)
87 {
88 $view = new ContentView(null, [], $parameters['viewType']);
89 $view->setIsEmbed($this->isEmbed($parameters));
90
91 if ($view->isEmbed() && $parameters['viewType'] === null) {
92 $view->setViewType(EmbedView::DEFAULT_VIEW_TYPE);
93 }
94
95 if (isset($parameters['location']) && $parameters['location'] instanceof Location) {
96 $location = $parameters['location'];
97 } elseif (isset($parameters['locationId'])) {
98 $location = $this->loadLocation($parameters['locationId']);
99 } else {
100 $location = null;
101 }
102
103 if (isset($parameters['content'])) {
104 $content = $parameters['content'];
105 } elseif ($location instanceof Location) {
106 // if we already have location load content true it so we avoid dual loading in case user does that in view
107 $content = $location->getContent();
108 if (!$this->canRead($content, $location, $view->isEmbed())) {
109 $missingPermission = 'read' . ($view->isEmbed() ? '|view_embed' : '');
110 throw new UnauthorizedException(
111 'content',
112 $missingPermission,
113 [
114 'contentId' => $content->id,
115 'locationId' => $location->id,
116 ]
117 );
118 }
119 } else {
120 if (isset($parameters['contentId'])) {
121 $contentId = $parameters['contentId'];
122 } elseif (isset($location)) {
123 $contentId = $location->contentId;
124 } else {
125 throw new InvalidArgumentException('Content', 'No content could be loaded from parameters');
126 }
127
128 $content = $view->isEmbed() ? $this->loadEmbeddedContent($contentId, $location) : $this->loadContent($contentId);
129 }
130
131 $view->setContent($content);
132
133 if (isset($location)) {
134 if ($location->contentId !== $content->id) {
135 throw new InvalidArgumentException('Location', 'Provided location does not belong to selected content');
136 }
137
138 if (isset($parameters['contentId']) && $location->contentId !== (int)$parameters['contentId']) {
139 throw new InvalidArgumentException(
140 'Location',
141 'Provided location does not belong to selected content as requested via contentId parameter'
142 );
143 }
144 } elseif (isset($this->locationLoader)) {
145 try {
146 $location = $this->locationLoader->loadLocation($content->contentInfo);
147 } catch (NotFoundException $e) {
148 // nothing else to do
149 }
150 }
151
152 if (isset($location)) {
153 $view->setLocation($location);
154 }
155
156 $this->viewParametersInjector->injectViewParameters($view, $parameters);
157 $this->viewConfigurator->configure($view);
158
159 // deprecated controller actions are replaced with their new equivalent, viewAction and embedAction
160 if (!$view->getControllerReference() instanceof ControllerReference) {
161 if (\in_array($parameters['_controller'], ['ez_content:viewLocation', 'ez_content:viewContent'])) {
162 $view->setControllerReference(new ControllerReference('ez_content:viewAction'));
163 } elseif (\in_array($parameters['_controller'], ['ez_content:embedLocation', 'ez_content:embedContent'])) {
164 $view->setControllerReference(new ControllerReference('ez_content:embedAction'));
165 }
166 }
167
168 return $view;
169 }
170
180 private function loadContent($contentId)
181 {
182 return $this->repository->getContentService()->loadContent($contentId);
183 }
184
197 private function loadEmbeddedContent($contentId, Location $location = null)
198 {
199 $content = $this->repository->sudo(
200 function (Repository $repository) use ($contentId) {
201 return $repository->getContentService()->loadContent($contentId);
202 }
203 );
204
205 if (!$this->canRead($content, $location)) {
206 throw new UnauthorizedException(
207 'content', 'read|view_embed',
208 ['contentId' => $contentId, 'locationId' => $location !== null ? $location->id : 'n/a']
209 );
210 }
211
212 // Check that Content is published, since sudo allows loading unpublished content.
213 if (
214 $content->getVersionInfo()->status !== VersionInfo::STATUS_PUBLISHED
215 && !$this->permissionResolver->canUser('content', 'versionread', $content)
216 ) {
217 throw new UnauthorizedException('content', 'versionread', ['contentId' => $contentId]);
218 }
219
220 return $content;
221 }
222
230 private function loadLocation($locationId)
231 {
232 $location = $this->repository->sudo(
233 function (Repository $repository) use ($locationId) {
234 return $repository->getLocationService()->loadLocation($locationId);
235 }
236 );
237
238 $request = $this->requestStack->getCurrentRequest();
239 if (!$request || !$request->attributes->get(PreviewController::PREVIEW_PARAMETER_NAME, false)) {
240 if ($location->invisible || $location->hidden) {
241 throw new HiddenLocationException($location, 'Location cannot be displayed as it is flagged as invisible.');
242 }
243 }
244
245 return $location;
246 }
247
257 private function canRead(Content $content, Location $location = null, bool $isEmbed = true): bool
258 {
259 $targets = isset($location) ? [$location] : [];
260
261 return
262 $this->permissionResolver->canUser('content', 'read', $content->contentInfo, $targets) ||
263 ($isEmbed && $this->permissionResolver->canUser('content', 'view_embed', $content->contentInfo, $targets));
264 }
265
274 private function isEmbed($parameters)
275 {
276 if ($parameters['_controller'] === 'ez_content:embedAction') {
277 return true;
278 }
279 if (\in_array($parameters['viewType'], ['embed', 'embed-inline'])) {
280 return true;
281 }
282
283 return false;
284 }
285 }
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\API\Repository\Values\Content\Content
Definition: Publish/API/Repository/Values/Content/Content.php:21
eZ\Publish\API\Repository\Values\Content\Location
Definition: API/Repository/Values/Content/Location.php:32
eZ\Publish\API\Repository\Values\Content\VersionInfo
Definition: API/Repository/Values/Content/VersionInfo.php:27
eZ\Publish\API\Repository\Values\Content\VersionInfo\STATUS_PUBLISHED
const STATUS_PUBLISHED
Definition: API/Repository/Values/Content/VersionInfo.php:29
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\Base\Exceptions\UnauthorizedException
UnauthorizedException Exception implementation.
Definition: Core/Base/Exceptions/UnauthorizedException.php:21
eZ\Publish\Core\MVC\Exception\HiddenLocationException
Definition: HiddenLocationException.php:13
eZ\Publish\Core\MVC\Symfony\Controller\Content\PreviewController
Definition: PreviewController.php:29
eZ\Publish\Core\MVC\Symfony\Controller\Content\PreviewController\PREVIEW_PARAMETER_NAME
const PREVIEW_PARAMETER_NAME
Definition: PreviewController.php:30
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder
Builds ContentView objects.
Definition: ContentViewBuilder.php:30
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\$locationLoader
$locationLoader
Definition: ContentViewBuilder.php:54
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\buildView
buildView(array $parameters)
Definition: ContentViewBuilder.php:86
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\$defaultTemplates
$defaultTemplates
Definition: ContentViewBuilder.php:51
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\loadContent
loadContent($contentId)
Loads Content with id $contentId.
Definition: ContentViewBuilder.php:180
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\$requestStack
$requestStack
Definition: ContentViewBuilder.php:44
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\$viewParametersInjector
$viewParametersInjector
Definition: ContentViewBuilder.php:41
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\loadLocation
loadLocation($locationId)
Loads a visible Location.
Definition: ContentViewBuilder.php:230
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\$permissionResolver
$permissionResolver
Definition: ContentViewBuilder.php:35
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\__construct
__construct(Repository $repository, Configurator $viewConfigurator, ParametersInjector $viewParametersInjector, RequestStack $requestStack, ContentInfoLocationLoader $locationLoader=null)
Definition: ContentViewBuilder.php:56
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\canRead
canRead(Content $content, Location $location=null, bool $isEmbed=true)
Checks if a user can read a content, or view it as an embed.
Definition: ContentViewBuilder.php:257
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\loadEmbeddedContent
loadEmbeddedContent($contentId, Location $location=null)
Loads the embedded content with id $contentId.
Definition: ContentViewBuilder.php:197
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\isEmbed
isEmbed($parameters)
Checks if the view is an embed one.
Definition: ContentViewBuilder.php:274
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\matches
matches($argument)
Tests if the builder matches the given argument.
Definition: ContentViewBuilder.php:71
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\$viewConfigurator
$viewConfigurator
Definition: ContentViewBuilder.php:38
eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder\$repository
$repository
Definition: ContentViewBuilder.php:32
eZ\Publish\Core\MVC\Symfony\View\ContentView
Main object to be rendered by the View Manager when viewing a content.
Definition: Publish/Core/MVC/Symfony/View/ContentView.php:40
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Publish\Core\Helper\ContentInfoLocationLoader
Loads a location based on a ContentInfo.
Definition: ContentInfoLocationLoader.php:15
eZ\Publish\Core\MVC\Symfony\View\Builder\ViewBuilder
Builds View objects based on an array of parameters.
Definition: ViewBuilder.php:15
eZ\Publish\Core\MVC\Symfony\View\Configurator
Configures a View object.
Definition: Configurator.php:15
eZ\Publish\Core\MVC\Symfony\View\EmbedView
A view that can embedded into another element.
Definition: EmbedView.php:13
eZ\Publish\Core\MVC\Symfony\View\EmbedView\DEFAULT_VIEW_TYPE
const DEFAULT_VIEW_TYPE
Definition: EmbedView.php:19
eZ\Publish\Core\MVC\Symfony\View\ParametersInjector
Collects parameters that will be injected into View objects.
Definition: ParametersInjector.php:13
eZ\Publish\Core\Helper\ContentInfoLocationLoader
Definition: SudoMainLocationLoader.php:7
eZ\Publish\Core\MVC\Symfony\View\Builder
Definition: BlockViewBuilder.php:7
eZ\Publish\Core\MVC\Symfony\View\Configurator
Definition: Configurator/ViewProvider.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.