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

FluidTYPO3 / vhs / 13566190336

27 Feb 2025 12:18PM UTC coverage: 72.127% (-0.6%) from 72.746%
13566190336

push

github

NamelessCoder
[TER] 7.1.0

5649 of 7832 relevant lines covered (72.13%)

20.01 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\Traits\TagViewHelperCompatibility;
13
use FluidTYPO3\Vhs\Utility\ContextUtility;
14
use FluidTYPO3\Vhs\Utility\RequestResolver;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
17
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
18

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

27
    /**
28
     * @var string
29
     */
30
    protected $tagName = 'link';
31

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

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

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

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

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

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

93
        $uri = $uriBuilder->build();
×
94

95
        if (empty($uri)) {
×
96
            return '';
×
97
        }
98

99
        $uri = $GLOBALS['TSFE']->baseUrlWrap($uri);
×
100

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

104
        $renderedTag = $this->tag->render();
×
105

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

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