• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CPS-IT / handlebars / 13521177515

25 Feb 2025 12:26PM UTC coverage: 88.385% (-4.9%) from 93.283%
13521177515

Pull #414

github

web-flow
Merge 65231cdd6 into 39f196173
Pull Request #414: [FEATURE] Introduce Handlebars view for Extbase context

0 of 66 new or added lines in 2 files covered. (0.0%)

1111 of 1257 relevant lines covered (88.39%)

4.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Classes/Extbase/View/ExtbaseHandlebarsViewResolver.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "handlebars".
7
 *
8
 * Copyright (C) 2025 Elias Häußler <e.haeussler@familie-redlich.de>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace Fr\Typo3Handlebars\Extbase\View;
25

26
use Psr\Container;
27
use TYPO3\CMS\Core;
28
use TYPO3\CMS\Extbase;
29
use TYPO3\CMS\Frontend;
30
use TYPO3Fluid\Fluid;
31

32
/**
33
 * ExtbaseHandlebarsViewResolver
34
 *
35
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
36
 * @license GPL-2.0-or-later
37
 */
38
final class ExtbaseHandlebarsViewResolver extends Extbase\Mvc\View\GenericViewResolver
39
{
40
    private readonly Frontend\ContentObject\ContentObjectRenderer $contentObjectRenderer;
41

NEW
42
    public function __construct(
×
43
        Container\ContainerInterface $container,
44
        private readonly Extbase\Configuration\ConfigurationManagerInterface $configurationManager,
45
        private readonly Core\TypoScript\TypoScriptService $typoScriptService,
46
    ) {
NEW
47
        parent::__construct($container);
×
48

NEW
49
        $this->contentObjectRenderer = $container->get(Frontend\ContentObject\ContentObjectRenderer::class);
×
50
    }
51

NEW
52
    public function resolve(
×
53
        string $controllerObjectName,
54
        string $actionName,
55
        string $format,
56
        bool $enableFallback = true,
57
    ): Fluid\View\ViewInterface {
NEW
58
        $handlebarsConfiguration = $this->resolveHandlebarsConfiguration($controllerObjectName, $actionName);
×
59

NEW
60
        if ($handlebarsConfiguration !== null || !$enableFallback) {
×
NEW
61
            return new ExtbaseHandlebarsView($this->contentObjectRenderer, $handlebarsConfiguration ?? []);
×
62
        }
63

NEW
64
        return parent::resolve($controllerObjectName, $actionName, $format);
×
65
    }
66

67
    /**
68
     * @return array<string, mixed>|null
69
     */
NEW
70
    private function resolveHandlebarsConfiguration(string $controllerObjectName, string $actionName): ?array
×
71
    {
NEW
72
        $configuration = $this->configurationManager->getConfiguration(
×
NEW
73
            Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
×
NEW
74
        );
×
NEW
75
        $controllerAlias = $configuration['controllerConfiguration'][$controllerObjectName]['alias'] ?? null;
×
76

77
        // Early return if controller is not properly registered
NEW
78
        if (!is_string($controllerAlias)) {
×
NEW
79
            return null;
×
80
        }
81

NEW
82
        $handlebarsConfiguration = $configuration['handlebars'] ?? null;
×
NEW
83
        $defaultConfiguration = [
×
NEW
84
            'templateName' => $controllerAlias . '/' . $actionName,
×
NEW
85
        ];
×
86

87
        // Early return if no handlebars configuration is available
NEW
88
        if (!is_array($handlebarsConfiguration)) {
×
NEW
89
            return $defaultConfiguration;
×
90
        }
91

92
        // HANDLEBARSTEMPLATE content object requires TypoScript configuration, so let's convert early
NEW
93
        $typoScriptConfiguration = $this->typoScriptService->convertPlainArrayToTypoScriptArray($handlebarsConfiguration);
×
94

95
        // Resolve template name from controller action
NEW
96
        if (is_string($typoScriptConfiguration['templateName'] ?? null) &&
×
NEW
97
            is_array($typoScriptConfiguration['templateName.'] ?? null)
×
98
        ) {
99
            // Inject custom fields to be referenced in TypoScript when resolving the
100
            // template name, e.g. in combination with a CASE content object
NEW
101
            $this->contentObjectRenderer->data['controllerName'] = $controllerAlias;
×
NEW
102
            $this->contentObjectRenderer->data['controllerObjectName'] = $controllerObjectName;
×
NEW
103
            $this->contentObjectRenderer->data['controllerAction'] = $actionName;
×
NEW
104
            $this->contentObjectRenderer->data['controllerNameAndAction'] = $controllerAlias . '::' . $actionName;
×
105

106
            try {
107
                // Resolve template name based on the current controller action
NEW
108
                $typoScriptConfiguration['templateName'] = $this->contentObjectRenderer->cObjGetSingle(
×
NEW
109
                    $typoScriptConfiguration['templateName'],
×
NEW
110
                    $typoScriptConfiguration['templateName.'],
×
NEW
111
                );
×
112
            } finally {
113
                // Remove configuration which is solely responsible for template name resolving
NEW
114
                unset(
×
NEW
115
                    $typoScriptConfiguration['templateName.'],
×
NEW
116
                    $this->contentObjectRenderer->data['controllerName'],
×
NEW
117
                    $this->contentObjectRenderer->data['controllerObjectName'],
×
NEW
118
                    $this->contentObjectRenderer->data['controllerAction'],
×
NEW
119
                    $this->contentObjectRenderer->data['controllerNameAndAction'],
×
NEW
120
                );
×
121
            }
122
        }
123

124
        // Early return if no (valid) template name is given
NEW
125
        if (!is_string($typoScriptConfiguration['templateName'] ?? null)) {
×
NEW
126
            return $defaultConfiguration;
×
127
        }
128

NEW
129
        return $typoScriptConfiguration;
×
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc