1 | <?php |
||
18 | class FieldBlockRenderer implements FieldBlockRendererInterface |
||
19 | { |
||
20 | const VIEW = 1; |
||
21 | const EDIT = 2; |
||
22 | |||
23 | const FIELD_VIEW_SUFFIX = '_field'; |
||
24 | const FIELD_EDIT_SUFFIX = '_field_edit'; |
||
25 | const FIELD_DEFINITION_VIEW_SUFFIX = '_settings'; |
||
26 | const FIELD_DEFINITION_EDIT_SUFFIX = '_field_definition_edit'; |
||
27 | |||
28 | /** |
||
29 | * @var Twig_Environment |
||
30 | */ |
||
31 | private $twig; |
||
32 | |||
33 | /** |
||
34 | * Array of Twig template resources for field view. |
||
35 | * Either the path to each template and its priority in a hash or its |
||
36 | * \Twig_Template (compiled) counterpart. |
||
37 | * |
||
38 | * @var Twig_Template[]|array |
||
39 | */ |
||
40 | private $fieldViewResources = []; |
||
41 | |||
42 | /** |
||
43 | * Array of Twig template resources for field edit. |
||
44 | * Either the path to each template and its priority in a hash or its |
||
45 | * \Twig_Template (compiled) counterpart. |
||
46 | * |
||
47 | * @var Twig_Template[]|array |
||
48 | */ |
||
49 | private $fieldEditResources = []; |
||
50 | |||
51 | /** |
||
52 | * Array of Twig template resources for field definition view. |
||
53 | * Either the path to each template and its priority in a hash or its |
||
54 | * \Twig_Template (compiled) counterpart. |
||
55 | * |
||
56 | * @var Twig_Template[]|array |
||
57 | */ |
||
58 | private $fieldDefinitionViewResources = []; |
||
59 | |||
60 | /** |
||
61 | * Array of Twig template resources for field definition edit. |
||
62 | * Either the path to each template and its priority in a hash or its |
||
63 | * \Twig_Template (compiled) counterpart. |
||
64 | * |
||
65 | * @var Twig_Template[]|array |
||
66 | */ |
||
67 | private $fieldDefinitionEditResources = []; |
||
68 | |||
69 | /** |
||
70 | * A \Twig_Template instance used to render template blocks, or path to the template to use. |
||
71 | * |
||
72 | * @var Twig_Template|string |
||
73 | */ |
||
74 | private $baseTemplate; |
||
75 | |||
76 | /** |
||
77 | * Template blocks. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | private $blocks = []; |
||
82 | |||
83 | /** |
||
84 | * @param Twig_Environment $twig |
||
85 | */ |
||
86 | public function setTwig(Twig_Environment $twig) |
||
90 | |||
91 | /** |
||
92 | * @param string|Twig_Template $baseTemplate |
||
93 | */ |
||
94 | public function setBaseTemplate($baseTemplate) |
||
98 | |||
99 | /** |
||
100 | * @param array $fieldViewResources |
||
101 | */ |
||
102 | public function setFieldViewResources(array $fieldViewResources = null) |
||
107 | |||
108 | /** |
||
109 | * @param array $fieldEditResources |
||
110 | */ |
||
111 | public function setFieldEditResources(array $fieldEditResources = null) |
||
116 | |||
117 | /** |
||
118 | * @param array $fieldDefinitionViewResources |
||
119 | */ |
||
120 | public function setFieldDefinitionViewResources(array $fieldDefinitionViewResources = null) |
||
125 | |||
126 | /** |
||
127 | * @param array $fieldDefinitionEditResources |
||
128 | */ |
||
129 | public function setFieldDefinitionEditResources(array $fieldDefinitionEditResources = null) |
||
134 | |||
135 | public function sortResourcesCallback(array $a, array $b) |
||
139 | |||
140 | public function renderContentFieldView(Field $field, $fieldTypeIdentifier, array $params = []) |
||
144 | |||
145 | public function renderContentFieldEdit(Field $field, $fieldTypeIdentifier, array $params = []) |
||
149 | |||
150 | /** |
||
151 | * @param Field $field |
||
152 | * @param string $fieldTypeIdentifier |
||
153 | * @param array $params |
||
154 | * @param int $type Either self::VIEW or self::EDIT |
||
155 | * |
||
156 | * @throws MissingFieldBlockException If no template block can be found for $field |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | private function renderContentField(Field $field, $fieldTypeIdentifier, array $params, $type) |
||
186 | |||
187 | public function renderFieldDefinitionView(FieldDefinition $fieldDefinition, array $params = []) |
||
191 | |||
192 | public function renderFieldDefinitionEdit(FieldDefinition $fieldDefinition, array $params = []) |
||
196 | |||
197 | /** |
||
198 | * @param FieldDefinition $fieldDefinition |
||
199 | * @param array $params |
||
200 | * @param int $type Either self::VIEW or self::EDIT |
||
201 | * |
||
202 | * @return string |
||
203 | */ |
||
204 | private function renderFieldDefinition(FieldDefinition $fieldDefinition, array $params, $type) |
||
224 | |||
225 | /** |
||
226 | * Returns the block named $blockName in the given template. If it's not |
||
227 | * found, returns null. |
||
228 | * |
||
229 | * @param string $blockName |
||
230 | * @param Twig_Template $tpl |
||
231 | * |
||
232 | * @return array|null |
||
233 | */ |
||
234 | private function searchBlock($blockName, Twig_Template $tpl) |
||
248 | |||
249 | /** |
||
250 | * Returns template blocks for $fieldTypeIdentifier. First check in the $localTemplate if it's provided. |
||
251 | * Template block convention name is <fieldTypeIdentifier>_field |
||
252 | * Example: 'ezstring_field' will be relevant for a full view of ezstring field type. |
||
253 | * |
||
254 | * @param string $fieldTypeIdentifier |
||
255 | * @param int $type Either self::VIEW or self::EDIT |
||
256 | * @param null|string|Twig_Template $localTemplate a file where to look for the block first |
||
257 | * |
||
258 | * @return array |
||
259 | */ |
||
260 | private function getBlocksByField($fieldTypeIdentifier, $type, $localTemplate = null) |
||
277 | |||
278 | /** |
||
279 | * Returns the template block for the settings of the field definition $definition. |
||
280 | * |
||
281 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $definition |
||
282 | * @param int $type Either self::VIEW or self::EDIT |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | private function getBlocksByFieldDefinition(FieldDefinition $definition, $type) |
||
293 | |||
294 | /** |
||
295 | * Returns the template block of the given $name available in the resources |
||
296 | * which name is $resourcesName. |
||
297 | * |
||
298 | * @param string $name |
||
299 | * @param string $resourcesName |
||
300 | * |
||
301 | * @return array |
||
302 | */ |
||
303 | private function getBlockByName($name, $resourcesName) |
||
326 | |||
327 | /** |
||
328 | * Returns expected block name for $fieldTypeIdentifier, attached in $content. |
||
329 | * |
||
330 | * @param string $fieldTypeIdentifier |
||
331 | * @param int $type Either self::VIEW or self::EDIT |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | private function getRenderFieldBlockName($fieldTypeIdentifier, $type) |
||
341 | |||
342 | /** |
||
343 | * Returns the name of the block to render the settings of the field |
||
344 | * definition $definition. |
||
345 | * |
||
346 | * @param string $fieldTypeIdentifier |
||
347 | * @param int $type Either self::VIEW or self::EDIT |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | private function getRenderFieldDefinitionBlockName($fieldTypeIdentifier, $type) |
||
357 | } |
||
358 |