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

FluidTYPO3 / vhs / 12958652765

24 Jan 2025 10:27PM UTC coverage: 72.509% (-0.02%) from 72.525%
12958652765

push

github

NamelessCoder
[BUGFIX] Set Request in UriBuilder wherever it is used

1 of 3 new or added lines in 3 files covered. (33.33%)

5539 of 7639 relevant lines covered (72.51%)

13.45 hits per line

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

32.0
/Classes/ViewHelpers/Page/Header/CanonicalViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Page\Header;
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\Traits\PageRendererTrait;
12
use FluidTYPO3\Vhs\Utility\ContextUtility;
13
use FluidTYPO3\Vhs\Utility\RequestResolver;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
16
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
17

18
/**
19
 * Returns the current canonical url in a link tag.
20
 */
21
class CanonicalViewHelper extends AbstractTagBasedViewHelper
22
{
23
    use PageRendererTrait;
24

25
    /**
26
     * @var string
27
     */
28
    protected $tagName = 'link';
29

30
    public function initializeArguments(): void
31
    {
32
        parent::initializeArguments();
6✔
33
        $this->registerUniversalTagAttributes();
6✔
34
        $this->registerArgument('pageUid', 'integer', 'The page uid to check', false, 0);
6✔
35
        $this->registerArgument(
6✔
36
            'queryStringMethod',
6✔
37
            'string',
6✔
38
            'From which place to add parameters. Values: "GET", "POST" and "GET,POST". See ' .
6✔
39
            'https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Typolink/Index.html, addQueryString.method',
6✔
40
            false,
6✔
41
            'GET'
6✔
42
        );
6✔
43
        $this->registerArgument(
6✔
44
            'normalWhenNoLanguage',
6✔
45
            'boolean',
6✔
46
            'DEPRECATED: Visibility is now handled by core\'s typolink function.'
6✔
47
        );
6✔
48
    }
49

50
    /**
51
     * @return string
52
     */
53
    public function render()
54
    {
55
        if (ContextUtility::isBackend()) {
×
56
            return '';
×
57
        }
58

59
        /** @var int $pageUid */
60
        $pageUid = $this->arguments['pageUid'];
×
61
        $pageUid = (integer) $pageUid;
×
62
        if (0 === $pageUid) {
×
63
            $pageUid = $GLOBALS['TSFE']->id;
×
64
        }
65

66
        /** @var string $queryStringMethod */
67
        $queryStringMethod = $this->arguments['queryStringMethod'];
×
68
        if (!in_array($queryStringMethod, ['GET', 'POST', 'GET,POST'], true)) {
×
69
            throw new \InvalidArgumentException(
×
70
                'The parameter "queryStringMethods" must be one of "GET", "POST" or "GET,POST".',
×
71
                1475337546
×
72
            );
×
73
        }
74

75
        /** @var UriBuilder $uriBuilder */
76
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
×
NEW
77
        $uriBuilder->setRequest(RequestResolver::resolveRequestFromRenderingContext($this->renderingContext));
×
78

79
        $uriBuilder = $uriBuilder->reset()
×
80
            ->setTargetPageUid($pageUid)
×
81
            ->setCreateAbsoluteUri(true)
×
82
            ->setAddQueryString(true)
×
83
            ->setArgumentsToBeExcludedFromQueryString(['id']);
×
84
        if (method_exists($uriBuilder, 'setAddQueryStringMethod')) {
×
85
            $uriBuilder->setAddQueryStringMethod($queryStringMethod);
×
86
        }
87
        if (method_exists($uriBuilder, 'setUseCacheHash')) {
×
88
            $uriBuilder->setUseCacheHash(true);
×
89
        }
90

91
        $uri = $uriBuilder->build();
×
92

93
        if (empty($uri)) {
×
94
            return '';
×
95
        }
96

97
        $uri = $GLOBALS['TSFE']->baseUrlWrap($uri);
×
98

99
        $this->tag->addAttribute('rel', 'canonical');
×
100
        $this->tag->addAttribute('href', $uri, false);
×
101

102
        $renderedTag = $this->tag->render();
×
103

104
        if (1 === (integer) $GLOBALS['TSFE']->config['config']['disableAllHeaderCode']) {
×
105
            return $renderedTag;
×
106
        }
107

108
        static::getPageRenderer()->addHeaderData($renderedTag);
×
109
        return '';
×
110
    }
111
}
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