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

FieldRenderingExtension.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension;
8
9 use eZ\Publish\API\Repository\Values\Content\Content;
10 use eZ\Publish\API\Repository\Values\Content\Field;
11 use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
12 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
13 use eZ\Publish\Core\Helper\TranslationHelper;
14 use eZ\Publish\Core\MVC\Symfony\FieldType\View\ParameterProviderRegistryInterface;
15 use eZ\Publish\Core\MVC\Symfony\Templating\FieldBlockRendererInterface;
16 use Twig_Environment;
17 use Twig_Extension;
18 use Twig_SimpleFunction;
19
23 class FieldRenderingExtension extends Twig_Extension
24 {
26 private $fieldBlockRenderer;
27
29 private $parameterProviderRegistry;
30
32 private $translationHelper;
33
39 private $fieldTypeIdentifiers = [];
40
41 public function __construct(
42 FieldBlockRendererInterface $fieldBlockRenderer,
43 ParameterProviderRegistryInterface $parameterProviderRegistry,
44 TranslationHelper $translationHelper
45 ) {
46 $this->fieldBlockRenderer = $fieldBlockRenderer;
47 $this->parameterProviderRegistry = $parameterProviderRegistry;
48 $this->translationHelper = $translationHelper;
49 }
50
51 public function getName()
52 {
53 return 'ezpublish.field_rendering';
54 }
55
56 public function getFunctions()
57 {
58 return [
59 new Twig_SimpleFunction(
60 'ez_render_field',
61 [$this, 'ezRenderFieldFunction'],
62 ['is_safe' => ['html'], 'needs_environment' => true]
63 ),
64 new Twig_SimpleFunction(
65 'ez_render_fielddefinition_settings',
66 [$this, 'ezRenderFieldDefinitionSettingsFunction'],
67 ['is_safe' => ['html'], 'needs_environment' => true]
68 ),
69 ];
70 }
71
72 public function ezRenderFieldFunction(Twig_Environment $environment, Content $content, $fieldIdentifier, array $params = [])
73 {
74 $this->fieldBlockRenderer->setTwig($environment);
75 return $this->renderField($content, $fieldIdentifier, $params);
76 }
77
78 public function ezRenderFieldDefinitionSettingsFunction(Twig_Environment $environment, FieldDefinition $fieldDefinition, array $params = [])
79 {
80 $this->fieldBlockRenderer->setTwig($environment);
81 return $this->renderFieldDefinitionSettings($fieldDefinition, $params);
82 }
83
92 public function renderFieldDefinitionSettings(FieldDefinition $fieldDefinition, array $params = [])
93 {
94 return $this->fieldBlockRenderer->renderFieldDefinitionView($fieldDefinition, $params);
95 }
96
108 public function renderField(Content $content, $fieldIdentifier, array $params = [])
109 {
110 $field = $this->translationHelper->getTranslatedField($content, $fieldIdentifier, isset($params['lang']) ? $params['lang'] : null);
111 if (!$field instanceof Field) {
112 throw new InvalidArgumentException(
113 '$fieldIdentifier',
114 "'{$fieldIdentifier}' field not present on content #{$content->contentInfo->id} '{$content->contentInfo->name}'"
115 );
116 }
117
118 $params = $this->getRenderFieldBlockParameters($content, $field, $params);
119 $fieldTypeIdentifier = $this->getFieldTypeIdentifier($content, $field);
120
121 return $this->fieldBlockRenderer->renderContentFieldView($field, $fieldTypeIdentifier, $params);
122 }
123
133 private function getRenderFieldBlockParameters(Content $content, Field $field, array $params = [])
134 {
135 // Merging passed parameters to default ones
136 $params += [
137 'parameters' => [], // parameters dedicated to template processing
138 'attr' => [], // attributes to add on the enclosing HTML tags
139 ];
140
141 $versionInfo = $content->getVersionInfo();
142 $contentInfo = $versionInfo->getContentInfo();
143 $contentType = $content->getContentType();
144 $fieldDefinition = $contentType->getFieldDefinition($field->fieldDefIdentifier);
145 // Adding Field, FieldSettings and ContentInfo objects to
146 // parameters to be passed to the template
147 $params += [
148 'field' => $field,
149 'content' => $content,
150 'contentInfo' => $contentInfo,
151 'versionInfo' => $versionInfo,
152 'fieldSettings' => $fieldDefinition->getFieldSettings(),
153 ];
154
155 // Adding field type specific parameters if any.
156 if ($this->parameterProviderRegistry->hasParameterProvider($fieldDefinition->fieldTypeIdentifier)) {
157 $params['parameters'] += $this->parameterProviderRegistry
158 ->getParameterProvider($fieldDefinition->fieldTypeIdentifier)
159 ->getViewParameters($field);
160 }
161
162 // make sure we can easily add class="<fieldtypeidentifier>-field" to the
163 // generated HTML
164 if (isset($params['attr']['class'])) {
165 $params['attr']['class'] .= ' ' . $this->getFieldTypeIdentifier($content, $field) . '-field';
166 } else {
167 $params['attr']['class'] = $this->getFieldTypeIdentifier($content, $field) . '-field';
168 }
169
170 return $params;
171 }
172
181 private function getFieldTypeIdentifier(Content $content, Field $field)
182 {
183 $contentType = $content->getContentType();
184 $key = $contentType->id . ' ' . $field->fieldDefIdentifier;
185
186 if (!isset($this->fieldTypeIdentifiers[$key])) {
187 $this->fieldTypeIdentifiers[$key] = $contentType
188 ->getFieldDefinition($field->fieldDefIdentifier)
189 ->fieldTypeIdentifier;
190 }
191
192 return $this->fieldTypeIdentifiers[$key];
193 }
194 }
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition
Definition: API/Repository/Values/ContentType/FieldDefinition.php:31
eZ\Publish\API\Repository\Values\Content\Content
Definition: Publish/API/Repository/Values/Content/Content.php:21
eZ\Publish\API\Repository\Values\Content\Content\getContentType
getContentType()
Returns the ContentType for this content.
eZ\Publish\API\Repository\Values\Content\Content\getVersionInfo
getVersionInfo()
Returns the VersionInfo for this version.
eZ\Publish\API\Repository\Values\Content\Field
Definition: API/Repository/Values/Content/Field.php:21
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\Helper\TranslationHelper
Helper class for translation.
Definition: TranslationHelper.php:25
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension
Twig extension for content fields/fieldDefinitions rendering (view and edit).
Definition: FieldRenderingExtension.php:24
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\$fieldBlockRenderer
$fieldBlockRenderer
Definition: FieldRenderingExtension.php:26
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\getFunctions
getFunctions()
Definition: FieldRenderingExtension.php:56
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\ezRenderFieldDefinitionSettingsFunction
ezRenderFieldDefinitionSettingsFunction(Twig_Environment $environment, FieldDefinition $fieldDefinition, array $params=[])
Definition: FieldRenderingExtension.php:78
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\getRenderFieldBlockParameters
getRenderFieldBlockParameters(Content $content, Field $field, array $params=[])
Generates the array of parameter to pass to the field template.
Definition: FieldRenderingExtension.php:133
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\__construct
__construct(FieldBlockRendererInterface $fieldBlockRenderer, ParameterProviderRegistryInterface $parameterProviderRegistry, TranslationHelper $translationHelper)
Definition: FieldRenderingExtension.php:41
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\getFieldTypeIdentifier
getFieldTypeIdentifier(Content $content, Field $field)
Returns the field type identifier for $field.
Definition: FieldRenderingExtension.php:181
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\getName
getName()
Definition: FieldRenderingExtension.php:51
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\renderField
renderField(Content $content, $fieldIdentifier, array $params=[])
Renders the HTML for a given field.
Definition: FieldRenderingExtension.php:108
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\$fieldTypeIdentifiers
$fieldTypeIdentifiers
Definition: FieldRenderingExtension.php:39
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\ezRenderFieldFunction
ezRenderFieldFunction(Twig_Environment $environment, Content $content, $fieldIdentifier, array $params=[])
Definition: FieldRenderingExtension.php:72
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\renderFieldDefinitionSettings
renderFieldDefinitionSettings(FieldDefinition $fieldDefinition, array $params=[])
Renders the HTML for the settings for the given field definition $definition.
Definition: FieldRenderingExtension.php:92
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\$translationHelper
$translationHelper
Definition: FieldRenderingExtension.php:32
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension\$parameterProviderRegistry
$parameterProviderRegistry
Definition: FieldRenderingExtension.php:29
eZ\Publish\Core\MVC\Symfony\FieldType\View\ParameterProviderRegistryInterface
Interface for fieldtypes view parameter provider registry.
Definition: ParameterProviderRegistryInterface.php:13
eZ\Publish\Core\MVC\Symfony\Templating\FieldBlockRendererInterface
Interface for content fields/fieldDefinitions renderers.
Definition: FieldBlockRendererInterface.php:18
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension
Definition: ContentExtension.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.