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

FluidTYPO3 / vhs / 14862037974

06 May 2025 02:18PM UTC coverage: 72.09% (-0.02%) from 72.109%
14862037974

push

github

NamelessCoder
[TER] 7.1.3

5649 of 7836 relevant lines covered (72.09%)

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

226
        return $output;
×
227
    }
228

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

271
        return is_scalar($output) ? (string) $output : '';
21✔
272
    }
273

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

335
        return implode(LF, $html);
21✔
336
    }
337

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

369
        return $html;
21✔
370
    }
371

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

388
        return $pageUid;
35✔
389
    }
390

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

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

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

472
        return $processedPages;
21✔
473
    }
474

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

486
        return $page['title'];
×
487
    }
488

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

516
    public function setOriginal(bool $original): void
517
    {
518
        $this->original = $original;
7✔
519
    }
520

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

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

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

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

596
        return $parentInstance;
14✔
597
    }
598

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

625
    public function getMenuArguments(): array
626
    {
627
        if (!is_array($this->arguments)) {
×
628
            return $this->arguments->toArray();
×
629
        }
630
        return $this->arguments;
×
631
    }
632

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

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

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

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

681
        return $pages;
35✔
682
    }
683
}
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