1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Templating\Twig\Extensions; |
6
|
|
|
|
7
|
|
|
use eZ\Publish\Core\MVC\ConfigResolverInterface; |
8
|
|
|
use Netgen\InformationCollection\API\Value\Attribute; |
9
|
|
|
use Netgen\InformationCollection\API\Value\Collection; |
10
|
|
|
use Twig\Environment; |
11
|
|
|
use Twig\TemplateWrapper; |
12
|
|
|
|
13
|
|
|
class FieldRenderingRuntime |
14
|
|
|
{ |
15
|
|
|
public const FIELD_VIEW_SUFFIX = '_field'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \Twig\Environment |
19
|
|
|
*/ |
20
|
|
|
protected $environment; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \eZ\Publish\Core\MVC\ConfigResolverInterface |
24
|
|
|
*/ |
25
|
|
|
protected $configResolver; |
26
|
|
|
|
27
|
|
|
public function __construct(Environment $environment, ConfigResolverInterface $configResolver) |
28
|
|
|
{ |
29
|
|
|
$this->environment = $environment; |
30
|
|
|
$this->configResolver = $configResolver; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function renderField(Collection $collection, Attribute $attribute): string |
34
|
|
|
{ |
35
|
|
|
$template = $this->getTemplate(); |
36
|
|
|
$blockName = $this->getRenderFieldBlockName($attribute); |
37
|
|
|
|
38
|
|
|
if (!$template->hasBlock($blockName)) { |
39
|
|
|
return $template->renderBlock($this->getDefaultRenderFieldBlockName(), ['collection' => $collection, 'attribute' => $attribute]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $template->renderBlock($blockName, ['collection' => $collection, 'attribute' => $attribute]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function getRenderFieldBlockName(Attribute $attribute): string |
46
|
|
|
{ |
47
|
|
|
$fieldTypeIdentifier = $attribute->getFieldDefinition()->fieldTypeIdentifier; |
48
|
|
|
|
49
|
|
|
return $fieldTypeIdentifier . self::FIELD_VIEW_SUFFIX; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function getDefaultRenderFieldBlockName(): string |
53
|
|
|
{ |
54
|
|
|
return 'default' . self::FIELD_VIEW_SUFFIX; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getTemplate(): TemplateWrapper |
58
|
|
|
{ |
59
|
|
|
return $this->environment->load('@NetgenInformationCollection/admin/content_fields.html.twig'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|