Completed
Branch master (d17104)
by Christian
21:20
created

HreflangGenerator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A generate() 0 16 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Seo\HrefLang;
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License, either version 2
11
 * of the License, or any later version.
12
 *
13
 * For the full copyright and license information, please read the
14
 * LICENSE.txt file that was distributed with this source code.
15
 *
16
 * The TYPO3 project - inspiring people to share!
17
 */
18
19
use TYPO3\CMS\Core\Site\Entity\Site;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
22
use TYPO3\CMS\Frontend\DataProcessing\LanguageMenuProcessor;
23
24
/**
25
 * Class to add the metatags for the SEO fields in core
26
 *
27
 * @internal
28
 */
29
class HreflangGenerator
30
{
31
    /**
32
     * The content object renderer
33
     *
34
     * @var ContentObjectRenderer
35
     */
36
    public $cObj;
37
38
    /**
39
     * HreflangGenerator constructor
40
     *
41
     * @param ContentObjectRenderer $cObj
42
     */
43
    public function __construct(ContentObjectRenderer $cObj = null)
44
    {
45
        if ($cObj === null) {
46
            $cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
47
        }
48
        $this->cObj = $cObj;
49
    }
50
51
    public function generate(): string
52
    {
53
        $hreflangs = '';
54
        if ($GLOBALS['TYPO3_REQUEST']->getAttribute('site') instanceof Site) {
55
            $languageMenu = GeneralUtility::makeInstance(LanguageMenuProcessor::class);
56
            $languages = $languageMenu->process($this->cObj, [], [], []);
57
            $hreflangs = '' . LF;
58
            foreach ($languages['languagemenu'] as $language) {
59
                if ($language['available'] == 1) {
60
                    $hreflangs .= '<link rel="alternate" hreflang="' . $language['hreflang'] . '" href="' . $language['link'] . '"/>' . LF;
61
                }
62
            }
63
            $hreflangs .= '<link rel="alternate" hreflang="x-default" href="' . $languages['languagemenu'][0]['link'] . '"/>' . LF;
64
            $GLOBALS['TSFE']->additionalHeaderData[] = $hreflangs;
65
        }
66
        return $hreflangs;
67
    }
68
}
69