Completed
Push — unit-test-view-helpers ( 96d65b...71f2bd )
by Romain
03:27
created

SectionViewHelper::injectSectionService()   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
rs 10
c 0
b 0
f 0
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\ViewHelpers\Service\FieldService;
17
use Romm\Formz\ViewHelpers\Service\SectionService;
18
use TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler;
19
use TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode;
20
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
21
22
/**
23
 * This view helper registers a section which will not be rendered directly, but
24
 * with the usage of the `RenderSection` view helper.
25
 *
26
 * It is used to manage dynamic parts of the layouts used with the `field` view
27
 * helper: every layout can call as many sections as it needs, and this sections
28
 * must then be registered using this view helper.
29
 */
30
class SectionViewHelper extends AbstractViewHelper implements CompilableInterface
31
{
32
    /**
33
     * @var FieldService
34
     */
35
    protected $fieldService;
36
37
    /**
38
     * @var SectionService
39
     */
40
    protected $sectionService;
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function initializeArguments()
46
    {
47
        $this->registerArgument('name', 'string', 'Name of the section.', true);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function render()
54
    {
55
        $this->fieldService->checkIsInsideFieldViewHelper();
56
57
        $this->sectionService->addSectionClosure($this->arguments['name'], $this->buildRenderChildrenClosure());
58
    }
59
60
    /**
61
     * In the created PHP code, we add a call to the function which will
62
     * register the closure to render this section.
63
     *
64
     * @inheritdoc
65
     */
66
    public function compile($argumentsVariableName, $renderChildrenClosureVariableName, &$initializationPhpCode, AbstractNode $syntaxTreeNode, TemplateCompiler $templateCompiler)
67
    {
68
        $initializationPhpCode .= SectionService::class . '::get()->addSectionClosure(' . $argumentsVariableName . "['name'], " . $renderChildrenClosureVariableName . ');' . LF;
69
70
        return '""';
71
    }
72
73
    /**
74
     * @param FieldService $service
75
     */
76
    public function injectFieldService(FieldService $service)
77
    {
78
        $this->fieldService = $service;
79
    }
80
81
    /**
82
     * @param SectionService $sectionService
83
     */
84
    public function injectSectionService(SectionService $sectionService)
85
    {
86
        $this->sectionService = $sectionService;
87
    }
88
}
89