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

Publish/Core/MVC/Symfony/View/Manager.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\MVC\Symfony\View;
8
9 use eZ\Publish\API\Repository\Values\Content\Content;
10 use eZ\Publish\API\Repository\Values\Content\Location;
11 use eZ\Publish\Core\FieldType\Page\Parts\Block;
12 use eZ\Publish\Core\MVC\Symfony\MVCEvents;
13 use eZ\Publish\Core\MVC\Symfony\Event\PreContentViewEvent;
14 use eZ\Publish\API\Repository\Repository;
15 use eZ\Publish\Core\MVC\ConfigResolverInterface;
16 use Symfony\Component\Templating\EngineInterface;
17 use Psr\Log\LoggerInterface;
18 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19 use RuntimeException;
20
21 class Manager implements ViewManagerInterface
22 {
24 protected $templateEngine;
25
27 protected $logger;
28
34 protected $contentViewProviders = [];
35
41 protected $locationViewProviders = [];
42
48 protected $blockViewProviders = [];
49
51 protected $sortedContentViewProviders;
52
54 protected $sortedLocationViewProviders;
55
57 protected $sortedBlockViewProviders;
58
60 protected $repository;
61
63 protected $eventDispatcher;
64
71 protected $viewBaseLayout;
72
74 protected $configResolver;
75
77 private $viewConfigurator;
78
79 public function __construct(
80 EngineInterface $templateEngine,
81 EventDispatcherInterface $eventDispatcher,
82 Repository $repository,
83 ConfigResolverInterface $configResolver,
84 $viewBaseLayout,
85 $viewConfigurator,
86 LoggerInterface $logger = null
87 ) {
88 $this->templateEngine = $templateEngine;
89 $this->eventDispatcher = $eventDispatcher;
90 $this->repository = $repository;
91 $this->configResolver = $configResolver;
92 $this->viewBaseLayout = $viewBaseLayout;
93 $this->logger = $logger;
94 $this->viewConfigurator = $viewConfigurator;
95 }
96
104 private function addViewProvider(&$property, $viewProvider, $priority)
105 {
106 $priority = (int)$priority;
107 if (!isset($property[$priority])) {
108 $property[$priority] = [];
109 }
110
111 $property[$priority][] = $viewProvider;
112 }
113
121 public function addContentViewProvider(ViewProvider $viewProvider, $priority = 0)
122 {
123 $this->addViewProvider($this->contentViewProviders, $viewProvider, $priority);
124 }
125
133 public function addLocationViewProvider(ViewProvider $viewProvider, $priority = 0)
134 {
135 $this->addViewProvider($this->locationViewProviders, $viewProvider, $priority);
136 }
137
145 public function addBlockViewProvider(ViewProvider $viewProvider, $priority = 0)
146 {
147 $this->addViewProvider($this->blockViewProviders, $viewProvider, $priority);
148 }
149
153 public function getAllContentViewProviders()
154 {
155 if (empty($this->sortedContentViewProviders)) {
156 $this->sortedContentViewProviders = $this->sortViewProviders($this->contentViewProviders);
157 }
158
159 return $this->sortedContentViewProviders;
160 }
161
165 public function getAllLocationViewProviders()
166 {
167 if (empty($this->sortedLocationViewProviders)) {
168 $this->sortedLocationViewProviders = $this->sortViewProviders($this->locationViewProviders);
169 }
170
171 return $this->sortedLocationViewProviders;
172 }
173
177 public function getAllBlockViewProviders()
178 {
179 if (empty($this->sortedBlockViewProviders)) {
180 $this->sortedBlockViewProviders = $this->sortViewProviders($this->blockViewProviders);
181 }
182
183 return $this->sortedBlockViewProviders;
184 }
185
194 protected function sortViewProviders($property)
195 {
196 $sortedViewProviders = [];
197 krsort($property);
198
199 foreach ($property as $viewProvider) {
200 $sortedViewProviders = array_merge($sortedViewProviders, $viewProvider);
201 }
202
203 return $sortedViewProviders;
204 }
205
220 public function renderContent(Content $content, $viewType = ViewManagerInterface::VIEW_TYPE_FULL, $parameters = [])
221 {
222 $view = new ContentView(null, $parameters, $viewType);
223 $view->setContent($content);
224 if (isset($parameters['location'])) {
225 $view->setLocation($parameters['location']);
226 }
227
228 $this->viewConfigurator->configure($view);
229
230 if ($view->getTemplateIdentifier() === null) {
231 throw new RuntimeException('Unable to find a template for #' . $content->contentInfo->id);
232 }
233
234 return $this->renderContentView($view, $parameters);
235 }
236
252 public function renderLocation(Location $location, $viewType = ViewManagerInterface::VIEW_TYPE_FULL, $parameters = [])
253 {
254 if (!isset($parameters['location'])) {
255 $parameters['location'] = $location;
256 }
257
258 if (!isset($parameters['content'])) {
259 $parameters['content'] = $this->repository->getContentService()->loadContentByContentInfo(
260 $location->contentInfo,
261 $this->configResolver->getParameter('languages')
262 );
263 }
264
265 return $this->renderContent($parameters['content'], $viewType, $parameters);
266 }
267
281 public function renderBlock(Block $block, $parameters = [])
282 {
283 $view = new BlockView(null, $parameters);
284 $view->setBlock($block);
285
286 $this->viewConfigurator->configure($view);
287
288 if ($view->getTemplateIdentifier() === null) {
289 throw new RuntimeException("Unable to find a view for location #$block->id");
290 }
291
292 return $this->renderContentView($view);
293 }
294
304 public function renderContentView(View $view, array $defaultParams = [])
305 {
306 $defaultParams['viewbaseLayout'] = $this->viewBaseLayout;
307 $view->addParameters($defaultParams);
308 $this->eventDispatcher->dispatch(
309 MVCEvents::PRE_CONTENT_VIEW,
310 new PreContentViewEvent($view)
311 );
312
313 $templateIdentifier = $view->getTemplateIdentifier();
314 $params = $view->getParameters();
315 if ($templateIdentifier instanceof \Closure) {
316 return $templateIdentifier($params);
317 }
318
319 return $this->templateEngine->render($templateIdentifier, $params);
320 }
321 }
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\Core\FieldType\Page\Parts\Block
Definition: FieldType/Page/Parts/Block.php:22
eZ\Publish\Core\MVC\Symfony\Event\PreContentViewEvent
The PreContentViewEvent allows you to inject additional parameters to a content view template.
Definition: PreContentViewEvent.php:36
eZ\Publish\Core\MVC\Symfony\MVCEvents
Definition: MVCEvents.php:10
eZ\Publish\Core\MVC\Symfony\MVCEvents\PRE_CONTENT_VIEW
const PRE_CONTENT_VIEW
The PRE_CONTENT_VIEW event occurs right before a view is rendered for a content, via the content view...
Definition: MVCEvents.php:27
eZ\Publish\Core\MVC\Symfony\View\BlockView
Definition: Publish/Core/MVC/Symfony/View/BlockView.php:12
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\Core\MVC\Symfony\View\Manager
Definition: Publish/Core/MVC/Symfony/View/Manager.php:22
eZ\Publish\Core\MVC\Symfony\View\Manager\$contentViewProviders
$contentViewProviders
Definition: Publish/Core/MVC/Symfony/View/Manager.php:34
eZ\Publish\Core\MVC\Symfony\View\Manager\$sortedBlockViewProviders
$sortedBlockViewProviders
Definition: Publish/Core/MVC/Symfony/View/Manager.php:57
eZ\Publish\Core\MVC\Symfony\View\Manager\getAllContentViewProviders
getAllContentViewProviders()
Definition: Publish/Core/MVC/Symfony/View/Manager.php:153
eZ\Publish\Core\MVC\Symfony\View\Manager\$sortedContentViewProviders
$sortedContentViewProviders
Definition: Publish/Core/MVC/Symfony/View/Manager.php:51
eZ\Publish\Core\MVC\Symfony\View\Manager\$eventDispatcher
$eventDispatcher
Definition: Publish/Core/MVC/Symfony/View/Manager.php:63
eZ\Publish\Core\MVC\Symfony\View\Manager\renderContent
renderContent(Content $content, $viewType=ViewManagerInterface::VIEW_TYPE_FULL, $parameters=[])
Renders $content by selecting the right template.
Definition: Publish/Core/MVC/Symfony/View/Manager.php:220
eZ\Publish\Core\MVC\Symfony\View\Manager\$viewConfigurator
$viewConfigurator
Definition: Publish/Core/MVC/Symfony/View/Manager.php:77
eZ\Publish\Core\MVC\Symfony\View\Manager\$blockViewProviders
$blockViewProviders
Definition: Publish/Core/MVC/Symfony/View/Manager.php:48
eZ\Publish\Core\MVC\Symfony\View\Manager\$configResolver
$configResolver
Definition: Publish/Core/MVC/Symfony/View/Manager.php:74
eZ\Publish\Core\MVC\Symfony\View\Manager\$logger
$logger
Definition: Publish/Core/MVC/Symfony/View/Manager.php:27
eZ\Publish\Core\MVC\Symfony\View\Manager\$locationViewProviders
$locationViewProviders
Definition: Publish/Core/MVC/Symfony/View/Manager.php:41
eZ\Publish\Core\MVC\Symfony\View\Manager\addViewProvider
addViewProvider(&$property, $viewProvider, $priority)
Helper for {.
Definition: Publish/Core/MVC/Symfony/View/Manager.php:104
eZ\Publish\Core\MVC\Symfony\View\Manager\addBlockViewProvider
addBlockViewProvider(ViewProvider $viewProvider, $priority=0)
Registers $viewProvider as a valid location view provider.
Definition: Publish/Core/MVC/Symfony/View/Manager.php:145
eZ\Publish\Core\MVC\Symfony\View\Manager\$sortedLocationViewProviders
$sortedLocationViewProviders
Definition: Publish/Core/MVC/Symfony/View/Manager.php:54
eZ\Publish\Core\MVC\Symfony\View\Manager\getAllLocationViewProviders
getAllLocationViewProviders()
Definition: Publish/Core/MVC/Symfony/View/Manager.php:165
eZ\Publish\Core\MVC\Symfony\View\Manager\sortViewProviders
sortViewProviders($property)
Sort the registered view providers by priority.
Definition: Publish/Core/MVC/Symfony/View/Manager.php:194
eZ\Publish\Core\MVC\Symfony\View\Manager\renderContentView
renderContentView(View $view, array $defaultParams=[])
Renders passed ContentView object via the template engine.
Definition: Publish/Core/MVC/Symfony/View/Manager.php:304
eZ\Publish\Core\MVC\Symfony\View\Manager\__construct
__construct(EngineInterface $templateEngine, EventDispatcherInterface $eventDispatcher, Repository $repository, ConfigResolverInterface $configResolver, $viewBaseLayout, $viewConfigurator, LoggerInterface $logger=null)
Definition: Publish/Core/MVC/Symfony/View/Manager.php:79
eZ\Publish\Core\MVC\Symfony\View\Manager\addContentViewProvider
addContentViewProvider(ViewProvider $viewProvider, $priority=0)
Registers $viewProvider as a valid content view provider.
Definition: Publish/Core/MVC/Symfony/View/Manager.php:121
eZ\Publish\Core\MVC\Symfony\View\Manager\$viewBaseLayout
$viewBaseLayout
Definition: Publish/Core/MVC/Symfony/View/Manager.php:71
eZ\Publish\Core\MVC\Symfony\View\Manager\$templateEngine
$templateEngine
Definition: Publish/Core/MVC/Symfony/View/Manager.php:24
eZ\Publish\Core\MVC\Symfony\View\Manager\$repository
$repository
Definition: Publish/Core/MVC/Symfony/View/Manager.php:60
eZ\Publish\Core\MVC\Symfony\View\Manager\getAllBlockViewProviders
getAllBlockViewProviders()
Definition: Publish/Core/MVC/Symfony/View/Manager.php:177
eZ\Publish\Core\MVC\Symfony\View\Manager\renderLocation
renderLocation(Location $location, $viewType=ViewManagerInterface::VIEW_TYPE_FULL, $parameters=[])
Renders $location by selecting the right template for $viewType.
Definition: Publish/Core/MVC/Symfony/View/Manager.php:252
eZ\Publish\Core\MVC\Symfony\View\Manager\renderBlock
renderBlock(Block $block, $parameters=[])
Renders $block by selecting the right template.
Definition: Publish/Core/MVC/Symfony/View/Manager.php:281
eZ\Publish\Core\MVC\Symfony\View\Manager\addLocationViewProvider
addLocationViewProvider(ViewProvider $viewProvider, $priority=0)
Registers $viewProvider as a valid location view provider.
Definition: Publish/Core/MVC/Symfony/View/Manager.php:133
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Publish\Core\MVC\ConfigResolverInterface
Interface for config resolvers.
Definition: ConfigResolverInterface.php:19
eZ\Publish\Core\MVC\Symfony\View\ViewManagerInterface
Definition: ViewManagerInterface.php:14
eZ\Publish\Core\MVC\Symfony\View\ViewManagerInterface\VIEW_TYPE_FULL
const VIEW_TYPE_FULL
Definition: ViewManagerInterface.php:15
eZ\Publish\Core\MVC\Symfony\View\ViewProvider
Definition: ViewProvider.php:10
eZ\Publish\Core\MVC\Symfony\View\View
Main interface for Views of Value objects (content, block...).
Definition: Publish/Core/MVC/Symfony/View/View.php:16
eZ\Publish\Core\MVC\Symfony\View\View\addParameters
addParameters(array $parameters)
Adds a hash of parameters to the existing parameters.
eZ\Publish\Core\MVC\Symfony\View\View\getParameters
getParameters()
Returns registered parameters.
eZ\Publish\Core\MVC\Symfony\View\View\getTemplateIdentifier
getTemplateIdentifier()
Returns the registered template identifier.
eZ\Publish\Core\MVC\Symfony\View
Definition: BaseView.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.