Skip to content

Commit 843ee31

Browse files
committed
Added option to show alternate language versions of pages
1 parent d4bdf1f commit 843ee31

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

Classes/Controller/SitemapController.php

+60-1
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,28 @@ class SitemapController {
1717
*/
1818
public $settings = [];
1919

20+
/**
21+
* @var array
22+
*/
23+
public $languages = [];
24+
2025
public function generateAction($content, array $params) {
2126
$this->settings = $params['settings.'];
2227
$this->settings['includeDoktypes'] = GeneralUtility::intExplode(',', $this->settings['includeDoktypes']);
2328

29+
if ($this->settings['showAlternateLanguages']) {
30+
$this->languages = $this->loadLanguages();
31+
}
32+
2433
$typolinkRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
2534

2635
$this->urls = [];
2736

2837
$GLOBALS['TSFE']->gr_list = '0,-1';
2938

3039
$page = $GLOBALS['TSFE']->sys_page->getPage($GLOBALS['TSFE']->rootLine[0]['uid']);
31-
$result = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
40+
$result = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
41+
$result .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">';
3242
$result .= $this->renderPages([$page]);
3343

3444
foreach ($this->settings['additionalLinks.'] as $configuration) {
@@ -56,6 +66,16 @@ public function generateAction($content, array $params) {
5666
return $result;
5767
}
5868

69+
protected function loadLanguages() {
70+
$languages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
71+
'*',
72+
'sys_language',
73+
'1' . $GLOBALS['TSFE']->sys_page->enableFields('sys_language')
74+
);
75+
$languages = array_column($languages, null, 'uid');
76+
return $languages;
77+
}
78+
5979
protected function getRecords($tableName, array $queryConfiguration) {
6080
$records = [];
6181

@@ -103,6 +123,12 @@ private function fetchRow($res) {
103123

104124
protected function renderPages(array $pages = []) {
105125
$result = '';
126+
127+
if ($this->settings['showAlternateLanguages']) {
128+
$pageUids = array_column($pages, 'uid');
129+
$pageTranslations = $this->getPageTranslations($pageUids);
130+
}
131+
106132
foreach ($pages as $page) {
107133
if (in_array($page['doktype'], $this->settings['includeDoktypes']) && !$page['no_search']) {
108134
if (!$GLOBALS['TSFE']->sys_language_uid || $page['_PAGES_OVERLAY']) {
@@ -114,6 +140,22 @@ protected function renderPages(array $pages = []) {
114140
$this->urls[] = $url;
115141

116142
$result .= '<url><loc>' . htmlspecialchars($url) . '</loc>';
143+
144+
if ($this->settings['showAlternateLanguages'] && $pageTranslations && $pageTranslations[$page['uid']]) {
145+
foreach ($pageTranslations[$page['uid']] as $translation) {
146+
$language = $this->languages[$translation];
147+
$hreflang = $language['language_isocode'];
148+
if ($hreflang !== $language['flag']) {
149+
$hreflang .= '-' . $language['flag'];
150+
}
151+
$alternateUrl = $this->cObj->typoLink_URL([
152+
'parameter' => $page['uid'],
153+
'additionalParams' => '&L=' . $translation,
154+
'forceAbsoluteUrl' => true,
155+
]);
156+
$result .= '<xhtml:link rel="alternate" hreflang="' . $hreflang . '" href="' . htmlspecialchars($alternateUrl) . '"/>';
157+
}
158+
}
117159
if ($page['SYS_LASTCHANGED']) {
118160
$result .= '<lastmod>' . date('c', $page['SYS_LASTCHANGED']) . '</lastmod>';
119161
}
@@ -132,6 +174,23 @@ protected function renderPages(array $pages = []) {
132174
return $result;
133175
}
134176

177+
protected function getPageTranslations(array $pageUids) {
178+
if (!$pageUids) {
179+
return [];
180+
}
181+
182+
$pageTranslations = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
183+
'*',
184+
'pages_language_overlay',
185+
'pid IN (' . implode(',', $pageUids) . ')' . $GLOBALS['TSFE']->sys_page->enableFields('pages_language_overlay')
186+
);
187+
$result = [];
188+
foreach ($pageTranslations as $translation) {
189+
$result[$translation['pid']][] = $translation['sys_language_uid'];
190+
}
191+
return $result;
192+
}
193+
135194
protected function getSubpages($pageUid) {
136195
$pages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
137196
'*',

ext_typoscript_setup.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ plugin.tx_vierwdgooglesitemap {
1313
# DOKTYPE_RECYCLER = 255;
1414
settings.includeDoktypes = 1
1515

16+
settings.showAlternateLanguages = 0
17+
1618
settings.additionalLinks {
1719
# products {
1820
# records {
1921
# table = tx_vierwddlw_domain_model_productcategory
2022
# select {
2123
# pidInList = 17
22-
# andWhere = sys_language_uid=0
24+
# where = sys_language_uid=0
2325
# languageField = 0
2426
# }
2527
# }

0 commit comments

Comments
 (0)