Completed
Push — unit-test-view-helpers ( 33f1c6...2824c7 )
by Romain
02:16
created

OptionViewHelper::injectFieldService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Formz project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\ViewHelpers;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\ViewHelpers\Service\FieldService;
18
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
19
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
20
21
/**
22
 * Use this view helper at the first level inside the Field view helper. It will
23
 * be added to the arguments list, which can then be used inside the field
24
 * template.
25
 *
26
 * It does not add any feature, but allows the Fluid template to be far more
27
 * readable, as you can see below:
28
 *
29
 * Without the Option view helper:
30
 *
31
 * ```
32
 *  <formz:field layout="..."
33
 *               arguments="{label: '{f:translate(key: \'my_lll_key\')}', foo: 'bar'}">
34
 *
35
 *      ...
36
 *  </formz:field>
37
 * ```
38
 *
39
 * With it:
40
 *
41
 * ```
42
 *  <formz:field layout="...">
43
 *      <formz:option name="label" value="{f:translate(key: 'my_lll_key')}" />
44
 *      <formz:option name="foo" value="bar" />
45
 *
46
 *      ...
47
 *  </formz:field>
48
 * ```
49
 */
50
class OptionViewHelper extends AbstractViewHelper implements CompilableInterface
51
{
52
    /**
53
     * @var FieldService
54
     */
55
    protected $fieldService;
56
57
    /**
58
     * @var array
59
     */
60
    protected static $options = [];
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function initializeArguments()
66
    {
67
        $this->registerArgument('name', 'string', 'Name of the option.', true);
68
        $this->registerArgument('value', 'string', 'Value of the option.', true);
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function render()
75
    {
76
        $this->fieldService->checkIsInsideFieldViewHelper();
77
78
        return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
85
    {
86
        /** @var FieldService $service */
87
        $service = Core::instantiate(FieldService::class);
88
89
        $service->setFieldOption($arguments['name'], $arguments['value']);
90
    }
91
92
    /**
93
     * @param FieldService $service
94
     */
95
    public function injectFieldService(FieldService $service)
96
    {
97
        $this->fieldService = $service;
98
    }
99
}
100