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

CPS-IT / handlebars / 20095472230

10 Dec 2025 10:30AM UTC coverage: 91.13% (-0.2%) from 91.362%
20095472230

Pull #503

github

web-flow
Merge e4a2ef503 into 372572ae6
Pull Request #503: [FEATURE] Allow merging of processed variables

4 of 7 new or added lines in 1 file covered. (57.14%)

976 of 1071 relevant lines covered (91.13%)

5.11 hits per line

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

86.96
/Classes/DataProcessing/ProcessVariablesProcessor.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "handlebars".
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17

18
namespace CPSIT\Typo3Handlebars\DataProcessing;
19

20
use CPSIT\Typo3Handlebars\Exception;
21
use CPSIT\Typo3Handlebars\Renderer;
22
use Symfony\Component\DependencyInjection;
23
use TYPO3\CMS\Core\Utility\ArrayUtility;
24
use TYPO3\CMS\Frontend;
25

26
/**
27
 * Data processor to process given variables, especially useful in combination with other data processors.
28
 *
29
 * Example (standalone):
30
 * =====================
31
 *
32
 * hero = HANDLEBARSTEMPLATE
33
 * hero {
34
 *   templateName = @hero
35
 *
36
 *   # ...
37
 *
38
 *   dataProcessing {
39
 *     10 = process-variables
40
 *     10 {
41
 *       if.isTrue.field = title
42
 *
43
 *       variables {
44
 *         header = TEXT
45
 *         header.field = title
46
 *
47
 *         teaser = TEXT
48
 *         teaser.field = teaser
49
 *         teaser.parseFunc < lib.parseFunc_RTE
50
 *
51
 *         # ...
52
 *       }
53
 *     }
54
 *   }
55
 * }
56
 *
57
 * Example (with other data processor):
58
 * ====================================
59
 *
60
 * accordion = HANDLEBARSTEMPLATE
61
 * accordion {
62
 *   templateName = @accordion
63
 *
64
 *   # ...
65
 *
66
 *   dataProcessing {
67
 *     10 = database-query
68
 *     10 {
69
 *       # ...
70
 *
71
 *       dataProcessing {
72
 *         10 = process-variables
73
 *         10 {
74
 *           table = tx_mysitepackage_domain_model_accordion_element
75
 *           as = accordionItem
76
 *           variables {
77
 *             template = @accordion-item
78
 *
79
 *             header = TEXT
80
 *             header.field = title
81
 *
82
 *             bodytext = TEXT
83
 *             bodytext.field = bodytext
84
 *             bodytext.parseFunc < lib.parseFunc_RTE
85
 *
86
 *             # ...
87
 *           }
88
 *         }
89
 *       }
90
 *     }
91
 *   }
92
 * }
93
 *
94
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
95
 * @license GPL-2.0-or-later
96
 */
97
#[DependencyInjection\Attribute\AutoconfigureTag('data.processor', ['identifier' => 'process-variables'])]
98
final readonly class ProcessVariablesProcessor implements Frontend\ContentObject\DataProcessorInterface
99
{
100
    /**
101
     * @param array<string, mixed> $contentObjectConfiguration
102
     * @param array<string, mixed> $processorConfiguration
103
     * @param array<string|int, mixed> $processedData
104
     * @return array<string|int, mixed>
105
     * @throws Frontend\ContentObject\Exception\ContentRenderingException
106
     * @throws Exception\ReservedVariableCannotBeUsed
107
     */
108
    public function process(
4✔
109
        Frontend\ContentObject\ContentObjectRenderer $cObj,
110
        array $contentObjectConfiguration,
111
        array $processorConfiguration,
112
        array $processedData,
113
    ): array {
114
        $data = $processorConfiguration['data.'] ?? $processedData['data'] ?? $cObj->data;
4✔
115
        $table = $processorConfiguration['table'] ?? $processedData['table'] ?? $cObj->getCurrentTable();
4✔
116
        $variables = $processorConfiguration['variables.'] ?? null;
4✔
117
        $as = $processorConfiguration['as'] ?? null;
4✔
118
        $merge = (bool)($processorConfiguration['merge'] ?? false);
4✔
119

120
        // Early return if no variables to process are configured
121
        if (!\is_array($variables)) {
4✔
122
            return $processedData;
1✔
123
        }
124

125
        // Use temporary cObj for processing
126
        $cObj = clone $cObj;
3✔
127
        $cObj->start($data, $table);
3✔
128

129
        // Early return if processing should be skipped according to a configured condition
130
        if (\is_array($processorConfiguration['if.'] ?? null) && !$cObj->checkIf($processorConfiguration['if.'])) {
3✔
131
            return $processedData;
1✔
132
        }
133

134
        // Process variables with temporary cObj
135
        $processor = Renderer\Variables\VariablesProcessor::for($cObj);
2✔
136
        $processedVariables = $processor->process($variables);
2✔
137

138
        // Apply processed variables, either override/merge processed data (if no target variable name is given)
139
        // or merge with processed data using given target variable name ("as")
140
        if ($as === null) {
2✔
141
            if ($merge) {
1✔
NEW
142
                ArrayUtility::mergeRecursiveWithOverrule($processedData, $processedVariables);
×
143
            } else {
144
                $processedData = $processedVariables;
1✔
145
            }
146
        } elseif ($merge) {
1✔
NEW
147
            $processedData[$as] ??= [];
×
148

NEW
149
            ArrayUtility::mergeRecursiveWithOverrule($processedData[$as], $processedVariables);
×
150
        } else {
151
            $processedData[$as] = $processedVariables;
1✔
152
        }
153

154
        return $processedData;
2✔
155
    }
156
}
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