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

FieldBlockRenderer.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig;
8
9 use eZ\Publish\API\Repository\Values\Content\Field;
10 use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
11 use eZ\Publish\Core\MVC\Symfony\Templating\Exception\MissingFieldBlockException;
12 use eZ\Publish\Core\MVC\Symfony\Templating\FieldBlockRendererInterface;
13 use Twig_Environment;
14 use Twig_Template;
15
16 class FieldBlockRenderer implements FieldBlockRendererInterface
17 {
18 const VIEW = 1;
19 const EDIT = 2;
20
21 const FIELD_VIEW_SUFFIX = '_field';
22 const FIELD_EDIT_SUFFIX = '_field_edit';
23 const FIELD_DEFINITION_VIEW_SUFFIX = '_settings';
24 const FIELD_DEFINITION_EDIT_SUFFIX = '_field_definition_edit';
25
27 private $twig;
28
36 private $fieldViewResources = [];
37
45 private $fieldEditResources = [];
46
54 private $fieldDefinitionViewResources = [];
55
63 private $fieldDefinitionEditResources = [];
64
70 private $baseTemplate;
71
77 private $blocks = [];
78
82 public function setTwig(Twig_Environment $twig)
83 {
84 $this->twig = $twig;
85 }
86
90 public function setBaseTemplate($baseTemplate)
91 {
92 $this->baseTemplate = $baseTemplate;
93 }
94
98 public function setFieldViewResources(array $fieldViewResources = null)
99 {
100 $this->fieldViewResources = (array)$fieldViewResources;
101 usort($this->fieldViewResources, [$this, 'sortResourcesCallback']);
102 }
103
107 public function setFieldEditResources(array $fieldEditResources = null)
108 {
109 $this->fieldEditResources = (array)$fieldEditResources;
110 usort($this->fieldEditResources, [$this, 'sortResourcesCallback']);
111 }
112
116 public function setFieldDefinitionViewResources(array $fieldDefinitionViewResources = null)
117 {
118 $this->fieldDefinitionViewResources = (array)$fieldDefinitionViewResources;
119 usort($this->fieldDefinitionViewResources, [$this, 'sortResourcesCallback']);
120 }
121
125 public function setFieldDefinitionEditResources(array $fieldDefinitionEditResources = null)
126 {
127 $this->fieldDefinitionEditResources = (array)$fieldDefinitionEditResources;
128 usort($this->fieldDefinitionEditResources, [$this, 'sortResourcesCallback']);
129 }
130
131 public function sortResourcesCallback(array $a, array $b)
132 {
133 return $b['priority'] - $a['priority'];
134 }
135
136 public function renderContentFieldView(Field $field, $fieldTypeIdentifier, array $params = [])
137 {
138 return $this->renderContentField($field, $fieldTypeIdentifier, $params, self::VIEW);
139 }
140
141 public function renderContentFieldEdit(Field $field, $fieldTypeIdentifier, array $params = [])
142 {
143 return $this->renderContentField($field, $fieldTypeIdentifier, $params, self::EDIT);
144 }
145
156 private function renderContentField(Field $field, $fieldTypeIdentifier, array $params, $type)
157 {
158 $localTemplate = null;
159 if (isset($params['template'])) {
160 // local override of the template
161 // this template is put on the top the templates stack
162 $localTemplate = $params['template'];
163 unset($params['template']);
164 }
165
166 $params += ['field' => $field];
167
168 // Getting instance of Twig_Template that will be used to render blocks
169 if (is_string($this->baseTemplate)) {
170 $this->baseTemplate = $this->twig->loadTemplate($this->baseTemplate);
171 }
172 $blockName = $this->getRenderFieldBlockName($fieldTypeIdentifier, $type);
173 $context = $this->twig->mergeGlobals($params);
174 $blocks = $this->getBlocksByField($fieldTypeIdentifier, $type, $localTemplate);
175
176 if (!$this->baseTemplate->hasBlock($blockName, $context, $blocks)) {
177 throw new MissingFieldBlockException("Cannot find '$blockName' template block.");
178 }
179
180 return $this->baseTemplate->renderBlock($blockName, $context, $blocks);
181 }
182
183 public function renderFieldDefinitionView(FieldDefinition $fieldDefinition, array $params = [])
184 {
185 return $this->renderFieldDefinition($fieldDefinition, $params, self::VIEW);
186 }
187
188 public function renderFieldDefinitionEdit(FieldDefinition $fieldDefinition, array $params = [])
189 {
190 return $this->renderFieldDefinition($fieldDefinition, $params, self::EDIT);
191 }
192
200 private function renderFieldDefinition(FieldDefinition $fieldDefinition, array $params, $type)
201 {
202 if (is_string($this->baseTemplate)) {
203 $this->baseTemplate = $this->twig->loadTemplate($this->baseTemplate);
204 }
205
206 $params += [
207 'fielddefinition' => $fieldDefinition,
208 'settings' => $fieldDefinition->getFieldSettings(),
209 ];
210 $blockName = $this->getRenderFieldDefinitionBlockName($fieldDefinition->fieldTypeIdentifier, $type);
211 $context = $this->twig->mergeGlobals($params);
212 $blocks = $this->getBlocksByFieldDefinition($fieldDefinition, $type);
213
214 if (!$this->baseTemplate->hasBlock($blockName, $context, $blocks)) {
215 return '';
216 }
217
218 return $this->baseTemplate->renderBlock($blockName, $context, $blocks);
219 }
220
230 private function searchBlock($blockName, Twig_Template $tpl)
231 {
232 // Current template might have parents, so we need to loop against
233 // them to find a matching block
234 do {
235 foreach ($tpl->getBlocks() as $name => $block) {
236 if ($name === $blockName) {
237 return $block;
238 }
239 }
240 } while (($tpl = $tpl->getParent([])) instanceof Twig_Template);
241
242 return null;
243 }
244
256 private function getBlocksByField($fieldTypeIdentifier, $type, $localTemplate = null)
257 {
258 $fieldBlockName = $this->getRenderFieldBlockName($fieldTypeIdentifier, $type);
259 if ($localTemplate !== null) {
260 // $localTemplate might be a Twig_Template instance already (e.g. using _self Twig keyword)
261 if (!$localTemplate instanceof Twig_Template) {
262 $localTemplate = $this->twig->loadTemplate($localTemplate);
263 }
264
265 $block = $this->searchBlock($fieldBlockName, $localTemplate);
266 if ($block !== null) {
267 return [$fieldBlockName => $block];
268 }
269 }
270
271 return $this->getBlockByName($fieldBlockName, $type === self::EDIT ? 'fieldEditResources' : 'fieldViewResources');
272 }
273
282 private function getBlocksByFieldDefinition(FieldDefinition $definition, $type)
283 {
284 return $this->getBlockByName(
285 $this->getRenderFieldDefinitionBlockName($definition->fieldTypeIdentifier, $type),
286 $type === self::EDIT ? 'fieldDefinitionEditResources' : 'fieldDefinitionViewResources'
287 );
288 }
289
299 private function getBlockByName($name, $resourcesName)
300 {
301 if (isset($this->blocks[$name])) {
302 return [$name => $this->blocks[$name]];
303 }
304
305 foreach ($this->{$resourcesName} as &$template) {
306 if (!$template instanceof Twig_Template) {
307 $template = $this->twig->loadTemplate($template['template']);
308 }
309
310 $tpl = $template;
311
312 $block = $this->searchBlock($name, $tpl);
313 if ($block !== null) {
314 $this->blocks[$name] = $block;
315
316 return [$name => $block];
317 }
318 }
319
320 return [];
321 }
322
331 private function getRenderFieldBlockName($fieldTypeIdentifier, $type)
332 {
333 $suffix = $type === self::EDIT ? self::FIELD_EDIT_SUFFIX : self::FIELD_VIEW_SUFFIX;
334
335 return $fieldTypeIdentifier . $suffix;
336 }
337
347 private function getRenderFieldDefinitionBlockName($fieldTypeIdentifier, $type)
348 {
349 $suffix = $type === self::EDIT ? self::FIELD_DEFINITION_EDIT_SUFFIX : self::FIELD_DEFINITION_VIEW_SUFFIX;
350
351 return $fieldTypeIdentifier . $suffix;
352 }
353 }
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition
Definition: API/Repository/Values/ContentType/FieldDefinition.php:31
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition\getFieldSettings
getFieldSettings()
This method returns settings for the field definition supported by the field type.
eZ\Publish\API\Repository\Values\Content\Field
Definition: API/Repository/Values/Content/Field.php:21
eZ\Publish\Core\MVC\Symfony\Templating\Exception\MissingFieldBlockException
Definition: MissingFieldBlockException.php:12
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer
Definition: FieldBlockRenderer.php:17
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\$fieldViewResources
$fieldViewResources
Definition: FieldBlockRenderer.php:36
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\EDIT
const EDIT
Definition: FieldBlockRenderer.php:19
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\$fieldDefinitionEditResources
$fieldDefinitionEditResources
Definition: FieldBlockRenderer.php:63
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\$blocks
$blocks
Definition: FieldBlockRenderer.php:77
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\sortResourcesCallback
sortResourcesCallback(array $a, array $b)
Definition: FieldBlockRenderer.php:131
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\setTwig
setTwig(Twig_Environment $twig)
Definition: FieldBlockRenderer.php:82
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\renderFieldDefinition
renderFieldDefinition(FieldDefinition $fieldDefinition, array $params, $type)
Definition: FieldBlockRenderer.php:200
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\getRenderFieldDefinitionBlockName
getRenderFieldDefinitionBlockName($fieldTypeIdentifier, $type)
Returns the name of the block to render the settings of the field definition $definition.
Definition: FieldBlockRenderer.php:347
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\renderContentField
renderContentField(Field $field, $fieldTypeIdentifier, array $params, $type)
Definition: FieldBlockRenderer.php:156
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\$fieldDefinitionViewResources
$fieldDefinitionViewResources
Definition: FieldBlockRenderer.php:54
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\getRenderFieldBlockName
getRenderFieldBlockName($fieldTypeIdentifier, $type)
Returns expected block name for $fieldTypeIdentifier, attached in $content.
Definition: FieldBlockRenderer.php:331
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\setFieldDefinitionViewResources
setFieldDefinitionViewResources(array $fieldDefinitionViewResources=null)
Definition: FieldBlockRenderer.php:116
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\setFieldEditResources
setFieldEditResources(array $fieldEditResources=null)
Definition: FieldBlockRenderer.php:107
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\renderFieldDefinitionEdit
renderFieldDefinitionEdit(FieldDefinition $fieldDefinition, array $params=[])
Renders the HTML edot markup for the given field definition.
Definition: FieldBlockRenderer.php:188
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\$baseTemplate
$baseTemplate
Definition: FieldBlockRenderer.php:70
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\$twig
$twig
Definition: FieldBlockRenderer.php:27
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\$fieldEditResources
$fieldEditResources
Definition: FieldBlockRenderer.php:45
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\FIELD_DEFINITION_EDIT_SUFFIX
const FIELD_DEFINITION_EDIT_SUFFIX
Definition: FieldBlockRenderer.php:24
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\setFieldDefinitionEditResources
setFieldDefinitionEditResources(array $fieldDefinitionEditResources=null)
Definition: FieldBlockRenderer.php:125
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\setFieldViewResources
setFieldViewResources(array $fieldViewResources=null)
Definition: FieldBlockRenderer.php:98
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\searchBlock
searchBlock($blockName, Twig_Template $tpl)
Returns the block named $blockName in the given template.
Definition: FieldBlockRenderer.php:230
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\getBlocksByFieldDefinition
getBlocksByFieldDefinition(FieldDefinition $definition, $type)
Returns the template block for the settings of the field definition $definition.
Definition: FieldBlockRenderer.php:282
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\FIELD_VIEW_SUFFIX
const FIELD_VIEW_SUFFIX
Definition: FieldBlockRenderer.php:21
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\FIELD_EDIT_SUFFIX
const FIELD_EDIT_SUFFIX
Definition: FieldBlockRenderer.php:22
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\FIELD_DEFINITION_VIEW_SUFFIX
const FIELD_DEFINITION_VIEW_SUFFIX
Definition: FieldBlockRenderer.php:23
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\renderContentFieldView
renderContentFieldView(Field $field, $fieldTypeIdentifier, array $params=[])
Renders the HTML view markup for a given field.
Definition: FieldBlockRenderer.php:136
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\renderFieldDefinitionView
renderFieldDefinitionView(FieldDefinition $fieldDefinition, array $params=[])
Renders the HTML view markup for the given field definition.
Definition: FieldBlockRenderer.php:183
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\renderContentFieldEdit
renderContentFieldEdit(Field $field, $fieldTypeIdentifier, array $params=[])
Renders the HTML edit markup for a given field.
Definition: FieldBlockRenderer.php:141
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\getBlocksByField
getBlocksByField($fieldTypeIdentifier, $type, $localTemplate=null)
Returns template blocks for $fieldTypeIdentifier.
Definition: FieldBlockRenderer.php:256
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\setBaseTemplate
setBaseTemplate($baseTemplate)
Definition: FieldBlockRenderer.php:90
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\VIEW
const VIEW
Definition: FieldBlockRenderer.php:18
eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer\getBlockByName
getBlockByName($name, $resourcesName)
Returns the template block of the given $name available in the resources which name is $resourcesName...
Definition: FieldBlockRenderer.php:299
eZ\Publish\Core\MVC\Symfony\Templating\FieldBlockRendererInterface
Interface for content fields/fieldDefinitions renderers.
Definition: FieldBlockRendererInterface.php:18
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.