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

FluidTYPO3 / vhs / 27842785131

19 Jun 2026 06:40PM UTC coverage: 70.695% (-0.005%) from 70.7%
27842785131

push

github

NamelessCoder
[TASK] Correct failing test expectations

5 of 7 new or added lines in 4 files covered. (71.43%)

215 existing lines in 10 files now uncovered.

5517 of 7804 relevant lines covered (70.69%)

14.92 hits per line

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

67.45
/Classes/ViewHelpers/Menu/AbstractMenuViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Menu;
3

4
/*
5
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10

11
use FluidTYPO3\Vhs\Service\PageService;
12
use FluidTYPO3\Vhs\Traits\PageRecordViewHelperTrait;
13
use FluidTYPO3\Vhs\Traits\TagViewHelperTrait;
14
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
17

18
/**
19
 * Base class for menu rendering ViewHelpers.
20
 */
21
abstract class AbstractMenuViewHelper extends AbstractTagBasedViewHelper
22
{
23
    use PageRecordViewHelperTrait;
24
    use TagViewHelperTrait;
25

26
    /**
27
     * @var string
28
     */
29
    protected $tagName = 'ul';
30

31
    /**
32
     * @var boolean
33
     */
34
    protected $escapeOutput = false;
35

36
    /**
37
     * @var PageService
38
     */
39
    protected $pageService;
40

41
    private bool $original = true;
42
    private array $backupValues = [];
43

44
    public function injectPageService(PageService $pageService): void
45
    {
46
        $this->pageService = $pageService;
49✔
47
    }
48

49
    public function initializeArguments(): void
50
    {
51
        parent::initializeArguments();
42✔
52
        $this->registerUniversalTagAttributes();
42✔
53
        $this->registerPageRecordArguments();
42✔
54
        $this->registerArgument('tagName', 'string', 'Tag name to use for enclosing container', false, 'ul');
42✔
55
        $this->registerArgument(
42✔
56
            'tagNameChildren',
42✔
57
            'string',
42✔
58
            'Tag name to use for child nodes surrounding links. If set to "a" enables non-wrapping mode.',
42✔
59
            false,
42✔
60
            'li'
42✔
61
        );
42✔
62
        $this->registerArgument('entryLevel', 'integer', 'Optional entryLevel TS equivalent of the menu', false, 0);
42✔
63
        $this->registerArgument(
42✔
64
            'levels',
42✔
65
            'integer',
42✔
66
            'Number of levels to render - setting this to a number higher than 1 (one) will expand menu ' .
42✔
67
            'items that are active, to a depth of $levels starting from $entryLevel',
42✔
68
            false,
42✔
69
            1
42✔
70
        );
42✔
71
        $this->registerArgument(
42✔
72
            'expandAll',
42✔
73
            'boolean',
42✔
74
            'If TRUE and $levels > 1 then expands all (not just the active) menu items which have submenus',
42✔
75
            false,
42✔
76
            false
42✔
77
        );
42✔
78
        $this->registerArgument('classFirst', 'string', 'Optional class name for the first menu elment', false, '');
42✔
79
        $this->registerArgument('classLast', 'string', 'Optional class name for the last menu elment', false, '');
42✔
80
        $this->registerArgument('classActive', 'string', 'Optional class name to add to active links', false, 'active');
42✔
81
        $this->registerArgument(
42✔
82
            'classCurrent',
42✔
83
            'string',
42✔
84
            'Optional class name to add to current link',
42✔
85
            false,
42✔
86
            'current'
42✔
87
        );
42✔
88
        $this->registerArgument(
42✔
89
            'substElementUid',
42✔
90
            'boolean',
42✔
91
            'Optional parameter for wrapping the link with the uid of the page',
42✔
92
            false,
42✔
93
            false
42✔
94
        );
42✔
95
        $this->registerArgument(
42✔
96
            'showHiddenInMenu',
42✔
97
            'boolean',
42✔
98
            'Include pages that are set to be hidden in menus',
42✔
99
            false,
42✔
100
            false
42✔
101
        );
42✔
102
        $this->registerArgument('showCurrent', 'boolean', 'If FALSE, does not display the current page', false, true);
42✔
103
        $this->registerArgument(
42✔
104
            'linkCurrent',
42✔
105
            'boolean',
42✔
106
            'If FALSE, does not wrap the current page in a link',
42✔
107
            false,
42✔
108
            true
42✔
109
        );
42✔
110
        $this->registerArgument(
42✔
111
            'linkActive',
42✔
112
            'boolean',
42✔
113
            'If FALSE, does not wrap with links the titles of pages that are active in the rootline',
42✔
114
            false,
42✔
115
            true
42✔
116
        );
42✔
117
        $this->registerArgument(
42✔
118
            'titleFields',
42✔
119
            'string',
42✔
120
            'CSV list of fields to use as link label - default is "nav_title,title", change to for example ' .
42✔
121
            '"tx_myext_somefield,subtitle,nav_title,title". The first field that contains text will be used. ' .
42✔
122
            'Field value resolved AFTER page field overlays.',
42✔
123
            false,
42✔
124
            'nav_title,title'
42✔
125
        );
42✔
126
        $this->registerArgument(
42✔
127
            'includeAnchorTitle',
42✔
128
            'boolean',
42✔
129
            'If TRUE, includes the page title as title attribute on the anchor.',
42✔
130
            false,
42✔
131
            true
42✔
132
        );
42✔
133
        $this->registerArgument(
42✔
134
            'includeSpacers',
42✔
135
            'boolean',
42✔
136
            'Wether or not to include menu spacers in the page select query',
42✔
137
            false,
42✔
138
            false
42✔
139
        );
42✔
140
        $this->registerArgument(
42✔
141
            'deferred',
42✔
142
            'boolean',
42✔
143
            'If TRUE, does not output the tag content UNLESS a v:page.menu.deferred child ViewHelper is both used ' .
42✔
144
            'and triggered. This allows you to create advanced conditions while still using automatic rendering',
42✔
145
            false,
42✔
146
            false
42✔
147
        );
42✔
148
        $this->registerArgument(
42✔
149
            'as',
42✔
150
            'string',
42✔
151
            'If used, stores the menu pages as an array in a variable named after this value and renders the tag ' .
42✔
152
            'content. If the tag content is empty automatic rendering is triggered.',
42✔
153
            false,
42✔
154
            'menu'
42✔
155
        );
42✔
156
        $this->registerArgument(
42✔
157
            'rootLineAs',
42✔
158
            'string',
42✔
159
            'If used, stores the menu root line as an array in a variable named according to this value and renders ' .
42✔
160
            'the tag content - which means automatic rendering is disabled if this attribute is used',
42✔
161
            false,
42✔
162
            'rootLine'
42✔
163
        );
42✔
164
        $this->registerArgument(
42✔
165
            'excludePages',
42✔
166
            'mixed',
42✔
167
            'Page UIDs to exclude from the menu. Can be CSV, array or an object implementing Traversable.',
42✔
168
            false,
42✔
169
            ''
42✔
170
        );
42✔
171
        $this->registerArgument(
42✔
172
            'forceAbsoluteUrl',
42✔
173
            'boolean',
42✔
174
            'If TRUE, the menu will be rendered with absolute URLs',
42✔
175
            false,
42✔
176
            false
42✔
177
        );
42✔
178
        $this->registerArgument(
42✔
179
            'doktypes',
42✔
180
            'mixed',
42✔
181
            'DEPRECATED: Please use typical doktypes for starting points like shortcuts.',
42✔
182
            false,
42✔
183
            ''
42✔
184
        );
42✔
185
        $this->registerArgument(
42✔
186
            'divider',
42✔
187
            'string',
42✔
188
            'Optional divider to insert between each menu item. Note that this does not mix well with automatic ' .
42✔
189
            'rendering due to the use of an ul > li structure'
42✔
190
        );
42✔
191
    }
192

193
    public function render(): ?string
194
    {
195
        /** @var int|null $entryLevel */
UNCOV
196
        $entryLevel = $this->arguments['entryLevel'];
×
197
        /** @var int|null $pageUid */
UNCOV
198
        $pageUid = $this->arguments['pageUid'];
×
199
        $pageUid = $pageUid > 0 ? (int) $pageUid : null;
×
UNCOV
200
        $pages = $this->getMenu($pageUid, (int) $entryLevel);
×
201
        $menu = $this->parseMenu($pages);
×
202
        $rootLine = $this->pageService->getRootLine(
×
203
            $pageUid,
×
204
            (bool) ($this->arguments['reverse'] ?? false)
×
205
        );
×
206
        $this->cleanupSubmenuVariables();
×
207
        $this->cleanTemplateVariableContainer();
×
208
        $this->backupVariables();
×
209
        $variableProvider = $this->renderingContext->getVariableProvider();
×
210
        /** @var string $as */
211
        $as = $this->arguments['as'];
×
212
        /** @var string $rootLineAs */
UNCOV
213
        $rootLineAs = $this->arguments['rootLineAs'];
×
214
        $variableProvider->add($as, $menu);
×
UNCOV
215
        $variableProvider->add($rootLineAs, $rootLine);
×
216
        $this->initalizeSubmenuVariables();
×
217
        $output = $this->renderContent($menu);
×
218
        $this->cleanupSubmenuVariables();
×
219
        $variableProvider->remove($as);
×
220
        $variableProvider->remove($rootLineAs);
×
221
        $this->restoreVariables();
×
222

223
        return $output;
×
224
    }
225

226
    /**
227
     * Renders the tag's content or if omitted auto
228
     * renders the menu for the provided arguments.
229
     */
230
    public function renderContent(array $menu): string
231
    {
232
        $deferredRendering = (bool) $this->arguments['deferred'];
21✔
233
        if (0 === count($menu) && !$deferredRendering) {
21✔
UNCOV
234
            return '';
×
235
        }
236
        if ($deferredRendering) {
21✔
237
            $tagContent = $this->autoRender($menu);
×
UNCOV
238
            $this->tag->setContent($tagContent);
×
UNCOV
239
            $deferredContent = $this->tag->render();
×
240
            $this->renderingContext->getViewHelperVariableContainer()->addOrUpdate(
×
241
                'FluidTYPO3\Vhs\ViewHelpers\Menu\AbstractMenuViewHelper',
×
242
                'deferredString',
×
243
                $deferredContent
×
244
            );
×
245
            $this->renderingContext->getViewHelperVariableContainer()->addOrUpdate(
×
246
                'FluidTYPO3\Vhs\ViewHelpers\Menu\AbstractMenuViewHelper',
×
247
                'deferredArray',
×
248
                $menu
×
249
            );
×
250
            $output = $this->renderChildren();
×
251
            $this->unsetDeferredVariableStorage();
×
252
        } else {
253
            $content = $this->renderChildren();
21✔
254
            if (!is_scalar($content)) {
21✔
255
                $content = '';
21✔
256
            } else {
UNCOV
257
                $content = (string) $content;
×
258
            }
259
            if (0 < mb_strlen(trim($content))) {
21✔
260
                $output = $content;
×
261
            } elseif ($this->arguments['hideIfEmpty']) {
21✔
UNCOV
262
                $output = '';
×
263
            } else {
264
                $output = $this->renderTag($this->getWrappingTagName(), $this->autoRender($menu));
21✔
265
            }
266
        }
267

268
        return is_scalar($output) ? (string) $output : '';
21✔
269
    }
270

271
    protected function autoRender(array $menu, int $level = 1): string
272
    {
273
        /** @var string $tagName */
274
        $tagName = $this->arguments['tagNameChildren'];
21✔
275
        $this->tag->setTagName($this->getWrappingTagName());
21✔
276
        $html = [];
21✔
277
        /** @var int $levels */
278
        $levels = $this->arguments['levels'];
21✔
279
        $levels = (int) $levels;
21✔
280
        $showCurrent = (bool) $this->arguments['showCurrent'];
21✔
281
        $expandAll = (bool) $this->arguments['expandAll'];
21✔
282
        $itemsRendered = 0;
21✔
283
        $numberOfItems = count($menu);
21✔
284
        foreach ($menu as $page) {
21✔
285
            if ($page['current'] && !$showCurrent) {
21✔
UNCOV
286
                continue;
×
287
            }
288
            $class = (trim($page['class']) !== '') ? ' class="' . trim($page['class']) . '"' : '';
21✔
289
            $elementId = ($this->arguments['substElementUid']) ? ' id="elem_' . $page['uid'] . '"' : '';
21✔
290
            if (!$this->isNonWrappingMode()) {
21✔
291
                $html[] = '<' . $tagName . $elementId . $class . '>';
21✔
292
            }
293
            $html[] = $this->renderItemLink($page);
21✔
294
            if (($page['active'] || $expandAll) && $page['hasSubPages'] && $level < $levels) {
21✔
UNCOV
295
                $subPages = $this->getMenu($page['uid']);
×
UNCOV
296
                $subMenu = $this->parseMenu($subPages);
×
UNCOV
297
                if (0 < count($subMenu)) {
×
298
                    /** @var string|null $className */
299
                    $className = $this->arguments['class'];
×
300
                    $renderedSubMenu = $this->autoRender($subMenu, $level + 1);
×
301
                    /** @var string|null $parentTagId */
302
                    $parentTagId = $this->tag->getAttribute('id') ?? $this->arguments['id'] ?? null;
×
303
                    if (!empty($parentTagId)) {
×
UNCOV
304
                        $this->tag->addAttribute('id', $parentTagId . '-lvl-' . $level);
×
305
                    }
306
                    $this->tag->setTagName($this->getWrappingTagName());
×
307
                    $this->tag->setContent($renderedSubMenu);
×
UNCOV
308
                    $this->tag->addAttribute(
×
309
                        'class',
×
310
                        (!empty($className) ? $className . ' lvl-' : 'lvl-') . $level
×
311
                    );
×
312
                    $html[] = $this->tag->render();
×
313
                    if (!empty($parentTagId)) {
×
314
                        $this->tag->addAttribute('id', $parentTagId);
×
315
                    }
316
                }
317
            }
318
            if (!$this->isNonWrappingMode()) {
21✔
319
                $html[] = '</' . $tagName . '>';
21✔
320
            }
321
            $itemsRendered++;
21✔
322
            if (isset($this->arguments['divider']) && $itemsRendered < $numberOfItems) {
21✔
UNCOV
323
                $divider = $this->arguments['divider'];
×
UNCOV
324
                if (!$this->isNonWrappingMode()) {
×
UNCOV
325
                    $html[] = '<' . $tagName . '>' . $divider . '</' . $tagName . '>';
×
326
                } else {
327
                    $html[] = $divider;
×
328
                }
329
            }
330
        }
331

332
        return implode(LF, $html);
21✔
333
    }
334

335
    protected function renderItemLink(array $page): string
336
    {
337
        $isSpacer = $page['doktype'] === PageRepository::DOKTYPE_SPACER;
21✔
338
        $isCurrent = (bool) $page['current'];
21✔
339
        $isActive = (bool) $page['active'];
21✔
340
        $linkCurrent = (bool) $this->arguments['linkCurrent'];
21✔
341
        $linkActive = (bool) $this->arguments['linkActive'];
21✔
342
        $includeAnchorTitle = (bool) $this->arguments['includeAnchorTitle'];
21✔
343
        $target = (!empty($page['target'])) ? ' target="' . $page['target'] . '"' : '';
21✔
344
        $class = (trim($page['class']) !== '') ? ' class="' . trim($page['class']) . '"' : '';
21✔
345
        if ($isSpacer || ($isCurrent && !$linkCurrent) || ($isActive && !$linkActive)) {
21✔
UNCOV
346
            $html = htmlspecialchars($page['linktext']);
×
347
        } elseif ($includeAnchorTitle) {
21✔
348
            $html = sprintf(
21✔
349
                '<a href="%s" title="%s"%s%s>%s</a>',
21✔
350
                $page['link'],
21✔
351
                htmlspecialchars($page['title']),
21✔
352
                $class,
21✔
353
                $target,
21✔
354
                htmlspecialchars($page['linktext'])
21✔
355
            );
21✔
356
        } else {
UNCOV
357
            $html = sprintf(
×
UNCOV
358
                '<a href="%s"%s%s>%s</a>',
×
UNCOV
359
                $page['link'],
×
360
                $class,
×
361
                $target,
×
362
                htmlspecialchars($page['linktext'])
×
363
            );
×
364
        }
365

366
        return $html;
21✔
367
    }
368

369
    protected function determineParentPageUid(?int $pageUid = null, ?int $entryLevel = 0): ?int
370
    {
371
        $rootLineData = $this->pageService->getRootLine();
35✔
372
        if (null === $pageUid) {
35✔
UNCOV
373
            if (null !== $entryLevel) {
×
UNCOV
374
                if ($entryLevel < 0) {
×
UNCOV
375
                    $entryLevel = count($rootLineData) - 1 + $entryLevel;
×
376
                }
377
                if (is_array($rootLineData[$entryLevel] ?? null)) {
×
378
                    $pageUid = $rootLineData[$entryLevel]['uid'] ?? null;
×
379
                }
380
            } else {
381
                $pageUid = $GLOBALS['TSFE']->id;
×
382
            }
383
        }
384

385
        return $pageUid;
35✔
386
    }
387

388
    public function getMenu(?int $pageUid = null, ?int $entryLevel = 0): array
389
    {
390
        $pageUid = $this->determineParentPageUid($pageUid, $entryLevel);
35✔
391
        if ($pageUid === null) {
35✔
UNCOV
392
            return [];
×
393
        }
394
        $showHiddenInMenu = (bool) $this->arguments['showHiddenInMenu'];
35✔
395
        $showAccessProtected = (bool) $this->arguments['showAccessProtected'];
35✔
396
        $includeSpacers = (bool) $this->arguments['includeSpacers'];
35✔
397
        $excludePages = $this->processPagesArgument($this->arguments['excludePages']);
35✔
398

399
        return $this->pageService->getMenu(
35✔
400
            $pageUid,
35✔
401
            $excludePages,
35✔
402
            $showHiddenInMenu,
35✔
403
            $includeSpacers,
35✔
404
            $showAccessProtected
35✔
405
        );
35✔
406
    }
407

408
    public function parseMenu(array $pages): array
409
    {
410
        $count = 0;
21✔
411
        $total = count($pages);
21✔
412
        $processedPages = [];
21✔
413
        foreach ($pages as $index => $page) {
21✔
414
            if (!is_array($page) || !isset($page['uid'])) {
21✔
UNCOV
415
                continue;
×
416
            }
417
            $count++;
21✔
418
            $class = [];
21✔
419
            $originalPageUid = $page['uid'];
21✔
420
            $showAccessProtected = (bool) $this->arguments['showAccessProtected'];
21✔
421
            if ($showAccessProtected) {
21✔
UNCOV
422
                $pages[$index]['accessProtected'] = $this->pageService->isAccessProtected($page);
×
UNCOV
423
                if ($pages[$index]['accessProtected']) {
×
UNCOV
424
                    $class[] = $this->arguments['classAccessProtected'];
×
425
                }
426
                $pages[$index]['accessGranted'] = $this->pageService->isAccessGranted($page);
×
427
                if ($pages[$index]['accessGranted'] && $this->pageService->isAccessProtected($page)) {
×
UNCOV
428
                    $class[] = $this->arguments['classAccessGranted'];
×
429
                }
430
            }
431
            $targetPage = $this->pageService->getShortcutTargetPage($page);
21✔
432
            if ($targetPage !== null) {
21✔
433
                if ($this->pageService->shouldUseShortcutTarget($this->arguments)) {
7✔
UNCOV
434
                    $pages[$index] = $targetPage;
×
435
                }
436
                if ($this->pageService->shouldUseShortcutUid($this->arguments)) {
7✔
437
                    $pages[$index]['uid'] = $targetPage['uid'];
×
438
                }
439
            }
440
            if ($this->pageService->isActive($originalPageUid)) {
21✔
441
                $pages[$index]['active'] = true;
21✔
442
                $class[] = $this->arguments['classActive'];
21✔
443
            } else {
UNCOV
444
                $pages[$index]['active'] = false;
×
445
            }
446
            if ($this->pageService->isCurrent($targetPage['uid'] ?? $page['uid'])) {
21✔
447
                $pages[$index]['current'] = true;
14✔
448
                $class[] = $this->arguments['classCurrent'];
14✔
449
            } else {
450
                 $pages[$index]['current'] = false;
7✔
451
            }
452
            $pages[$index]['hasSubPages'] = false;
21✔
453
            if (0 < count($this->getMenu($originalPageUid))) {
21✔
454
                $pages[$index]['hasSubPages'] = true;
21✔
455
            }
456
            if (1 === $count) {
21✔
457
                $class[] = $this->arguments['classFirst'];
21✔
458
            }
459
            if ($count === $total) {
21✔
460
                $class[] = $this->arguments['classLast'];
21✔
461
            }
462
            $pages[$index]['class'] = implode(' ', $class);
21✔
463
            $pages[$index]['linktext'] = $this->getItemTitle($pages[$index]);
21✔
464
            $forceAbsoluteUrl = (bool) $this->arguments['forceAbsoluteUrl'];
21✔
465
            $pages[$index]['link'] = $this->pageService->getItemLink($pages[$index], $forceAbsoluteUrl);
21✔
466
            $processedPages[$index] = $pages[$index];
21✔
467
        }
468

469
        return $processedPages;
21✔
470
    }
471

472
    protected function getItemTitle(array $page): string
473
    {
474
        /** @var string $titleFieldsArgument */
475
        $titleFieldsArgument = $this->arguments['titleFields'];
21✔
476
        $titleFieldList = GeneralUtility::trimExplode(',', $titleFieldsArgument);
21✔
477
        foreach ($titleFieldList as $titleFieldName) {
21✔
478
            if (!empty($page[$titleFieldName])) {
21✔
479
                return $page[$titleFieldName];
21✔
480
            }
481
        }
482

UNCOV
483
        return $page['title'];
×
484
    }
485

486
    /**
487
     * Initialize variables used by the submenu instance recycler. Variables set here
488
     * may be read by the Page / Menu / Sub ViewHelper which then automatically repeats
489
     * rendering using the exact same arguments but with a new page UID as starting page.
490
     * Note that the submenu VieWHelper is only capable of recycling one type of menu at
491
     * a time - for example, a List menu nested inside a regular Menu ViewHelper will
492
     * simply start another menu rendering completely separate from the parent menu.
493
     */
494
    protected function initalizeSubmenuVariables(): void
495
    {
UNCOV
496
        if (!$this->original) {
×
UNCOV
497
            return;
×
498
        }
499
        $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
×
500
        $variables = $this->renderingContext->getVariableProvider()->getAll();
×
UNCOV
501
        $viewHelperVariableContainer->addOrUpdate(
×
502
            'FluidTYPO3\Vhs\ViewHelpers\Menu\AbstractMenuViewHelper',
×
503
            'parentInstance',
×
504
            $this
×
505
        );
×
506
        $viewHelperVariableContainer->addOrUpdate(
×
507
            'FluidTYPO3\Vhs\ViewHelpers\Menu\AbstractMenuViewHelper',
×
508
            'variables',
×
509
            $variables
×
510
        );
×
511
    }
512

513
    public function setOriginal(bool $original): void
514
    {
515
        $this->original = $original;
7✔
516
    }
517

518
    protected function cleanupSubmenuVariables(): void
519
    {
UNCOV
520
        if (!$this->original) {
×
UNCOV
521
            return;
×
522
        }
523
        $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
×
524
        if (!$viewHelperVariableContainer->exists(AbstractMenuViewHelper::class, 'parentInstance')) {
×
UNCOV
525
            return;
×
526
        }
527
        $viewHelperVariableContainer->remove(AbstractMenuViewHelper::class, 'parentInstance');
×
528
        $viewHelperVariableContainer->remove(AbstractMenuViewHelper::class, 'variables');
×
529
    }
530

531
    /**
532
     * Saves copies of all template variables while rendering
533
     * the menu.
534
     */
535
    public function backupVariables(): void
536
    {
537
        /** @var string $as */
538
        $as = $this->arguments['as'];
21✔
539
        /** @var string $rootLineAs */
540
        $rootLineAs = $this->arguments['rootLineAs'];
21✔
541
        $variableProvider = $this->renderingContext->getVariableProvider();
21✔
542
        $backups = [$as, $rootLineAs];
21✔
543
        foreach ($backups as $var) {
21✔
544
            if ($variableProvider->exists($var)) {
21✔
UNCOV
545
                $this->backupValues[$var] = $variableProvider->get($var);
×
UNCOV
546
                $variableProvider->remove($var);
×
547
            }
548
        }
549
    }
550

551
    /**
552
     * Restores all saved template variables.
553
     */
554
    public function restoreVariables(): void
555
    {
556
        $variableProvider = $this->renderingContext->getVariableProvider();
21✔
557
        if (0 < count($this->backupValues)) {
21✔
UNCOV
558
            foreach ($this->backupValues as $var => $value) {
×
UNCOV
559
                if (!$variableProvider->exists($var)) {
×
UNCOV
560
                    $variableProvider->add($var, $value);
×
561
                }
562
            }
563
        }
564
    }
565

566
    /**
567
     * Retrieves a stored, if any, parent instance of a menu. Although only implemented by
568
     * the Page / Menu / Sub ViewHelper, placing this method in this abstract class instead
569
     * will allow custom menu ViewHelpers to work as sub menu ViewHelpers without being
570
     * forced to implement their own variable retrieval or subclass Page / Menu / Sub.
571
     * Returns NULL if no parent exists.
572
     *
573
     * @param integer $pageUid UID of page that's the new parent page, overridden in arguments of cloned and
574
     *                         recycled menu ViewHelper instance.
575
     */
576
    protected function retrieveReconfiguredParentMenuInstance(int $pageUid): ?self
577
    {
578
        if (!$this->renderingContext->getViewHelperVariableContainer()->exists(
21✔
579
            AbstractMenuViewHelper::class,
21✔
580
            'parentInstance'
21✔
581
        )) {
21✔
582
            return null;
7✔
583
        }
584
        /** @var AbstractMenuViewHelper $parentInstance */
585
        $parentInstance = $this->renderingContext->getViewHelperVariableContainer()->get(
14✔
586
            AbstractMenuViewHelper::class,
14✔
587
            'parentInstance'
14✔
588
        );
14✔
589
        $arguments = $parentInstance->getMenuArguments();
14✔
590
        $arguments['pageUid'] = $pageUid;
14✔
591
        $parentInstance->setArguments($arguments);
14✔
592

593
        return $parentInstance;
14✔
594
    }
595

596
    protected function cleanTemplateVariableContainer(): void
597
    {
UNCOV
598
        if (!$this->renderingContext->getViewHelperVariableContainer()->exists(
×
UNCOV
599
            AbstractMenuViewHelper::class,
×
UNCOV
600
            'variables'
×
601
        )) {
×
602
            return;
×
603
        }
604
        /** @var iterable $storedVariables */
605
        $storedVariables = $this->renderingContext->getViewHelperVariableContainer()->get(
×
UNCOV
606
            AbstractMenuViewHelper::class,
×
UNCOV
607
            'variables'
×
608
        );
×
609
        $variableProvider = $this->renderingContext->getVariableProvider();
×
610
        /** @var iterable $allVariables */
611
        $allVariables = $variableProvider->getAll();
×
612
        foreach ($allVariables as $variableName => $value) {
×
UNCOV
613
            $this->backupValues[$variableName] = $value;
×
614
            $variableProvider->remove($variableName);
×
615
        }
616
        foreach ($storedVariables as $variableName => $value) {
×
617
            /** @var string $variableName */
UNCOV
618
            $variableProvider->add($variableName, $value);
×
619
        }
620
    }
621

622
    public function getMenuArguments(): array
623
    {
UNCOV
624
        if (!is_array($this->arguments)) {
×
UNCOV
625
            return $this->arguments->toArray();
×
626
        }
627
        return $this->arguments;
×
628
    }
629

630
    protected function unsetDeferredVariableStorage(): void
631
    {
632
        $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
7✔
633
        if ($viewHelperVariableContainer->exists(AbstractMenuViewHelper::class, 'deferredString')) {
7✔
634
            $viewHelperVariableContainer->remove(AbstractMenuViewHelper::class, 'deferredString');
7✔
635
            $viewHelperVariableContainer->remove(AbstractMenuViewHelper::class, 'deferredArray');
7✔
636
        }
637
    }
638

639
    public function getWrappingTagName(): string
640
    {
641
        /** @var string $tagName */
642
        $tagName = $this->arguments['tagName'];
21✔
643
        return $this->isNonWrappingMode() ? 'nav' : $tagName;
21✔
644
    }
645

646
    /**
647
     * Returns TRUE for non-wrapping mode which is triggered
648
     * by setting tagNameChildren to 'a'.
649
     */
650
    public function isNonWrappingMode(): bool
651
    {
652
        /** @var string $tagName */
653
        $tagName = $this->arguments['tagNameChildren'];
21✔
654
        return ('a' === strtolower($tagName));
21✔
655
    }
656

657
    /**
658
     * Returns array of page UIDs from provided pages
659
     *
660
     * @param mixed|null $pages
661
     */
662
    public function processPagesArgument($pages = null): array
663
    {
664
        if (null === $pages) {
49✔
665
            $pages = $this->arguments['pages'];
28✔
666
        }
667
        if ($pages instanceof \Traversable) {
49✔
UNCOV
668
            $pages = iterator_to_array($pages);
×
669
        } elseif (is_string($pages)) {
49✔
670
            $pages = GeneralUtility::trimExplode(',', $pages, true);
35✔
671
        } elseif (is_int($pages)) {
28✔
UNCOV
672
            $pages = (array) $pages;
×
673
        }
674
        if (!is_array($pages)) {
49✔
675
            return [];
14✔
676
        }
677

678
        return $pages;
35✔
679
    }
680
}
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