@@ -17,18 +17,28 @@ class SitemapController {
17
17
*/
18
18
public $ settings = [];
19
19
20
+ /**
21
+ * @var array
22
+ */
23
+ public $ languages = [];
24
+
20
25
public function generateAction ($ content , array $ params ) {
21
26
$ this ->settings = $ params ['settings. ' ];
22
27
$ this ->settings ['includeDoktypes ' ] = GeneralUtility::intExplode (', ' , $ this ->settings ['includeDoktypes ' ]);
23
28
29
+ if ($ this ->settings ['showAlternateLanguages ' ]) {
30
+ $ this ->languages = $ this ->loadLanguages ();
31
+ }
32
+
24
33
$ typolinkRenderer = GeneralUtility::makeInstance (ContentObjectRenderer::class);
25
34
26
35
$ this ->urls = [];
27
36
28
37
$ GLOBALS ['TSFE ' ]->gr_list = '0,-1 ' ;
29
38
30
39
$ 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"> ' ;
32
42
$ result .= $ this ->renderPages ([$ page ]);
33
43
34
44
foreach ($ this ->settings ['additionalLinks. ' ] as $ configuration ) {
@@ -56,6 +66,16 @@ public function generateAction($content, array $params) {
56
66
return $ result ;
57
67
}
58
68
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
+
59
79
protected function getRecords ($ tableName , array $ queryConfiguration ) {
60
80
$ records = [];
61
81
@@ -103,6 +123,12 @@ private function fetchRow($res) {
103
123
104
124
protected function renderPages (array $ pages = []) {
105
125
$ result = '' ;
126
+
127
+ if ($ this ->settings ['showAlternateLanguages ' ]) {
128
+ $ pageUids = array_column ($ pages , 'uid ' );
129
+ $ pageTranslations = $ this ->getPageTranslations ($ pageUids );
130
+ }
131
+
106
132
foreach ($ pages as $ page ) {
107
133
if (in_array ($ page ['doktype ' ], $ this ->settings ['includeDoktypes ' ]) && !$ page ['no_search ' ]) {
108
134
if (!$ GLOBALS ['TSFE ' ]->sys_language_uid || $ page ['_PAGES_OVERLAY ' ]) {
@@ -114,6 +140,22 @@ protected function renderPages(array $pages = []) {
114
140
$ this ->urls [] = $ url ;
115
141
116
142
$ 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
+ }
117
159
if ($ page ['SYS_LASTCHANGED ' ]) {
118
160
$ result .= '<lastmod> ' . date ('c ' , $ page ['SYS_LASTCHANGED ' ]) . '</lastmod> ' ;
119
161
}
@@ -132,6 +174,23 @@ protected function renderPages(array $pages = []) {
132
174
return $ result ;
133
175
}
134
176
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
+
135
194
protected function getSubpages ($ pageUid ) {
136
195
$ pages = $ GLOBALS ['TYPO3_DB ' ]->exec_SELECTgetRows (
137
196
'* ' ,
0 commit comments