-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathLanguageMenuViewHelper.php
More file actions
537 lines (494 loc) · 19.2 KB
/
LanguageMenuViewHelper.php
File metadata and controls
537 lines (494 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Page;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\Proxy\DoctrineQueryProxy;
use FluidTYPO3\Vhs\Proxy\SiteFinderProxy;
use FluidTYPO3\Vhs\Traits\ArrayConsumingViewHelperTrait;
use FluidTYPO3\Vhs\Traits\TagViewHelperCompatibility;
use FluidTYPO3\Vhs\Utility\ContentObjectFetcher;
use FluidTYPO3\Vhs\Utility\CoreUtility;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\LanguageAspect;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Site\Site;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
/**
* ViewHelper for rendering TYPO3 menus in Fluid
* Require the extension static_info_table.
*/
class LanguageMenuViewHelper extends AbstractTagBasedViewHelper
{
use ArrayConsumingViewHelperTrait;
use TagViewHelperCompatibility;
protected array $languageMenu = [];
protected int $defaultLangUid = 0;
/**
* @var string
*/
protected $tagName = 'ul';
/**
* @var ContentObjectRenderer
*/
protected $cObj;
protected ConfigurationManagerInterface $configurationManager;
/**
* @var Site|\TYPO3\CMS\Core\Site\Entity\Site
*/
protected $site;
public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void
{
$this->configurationManager = $configurationManager;
}
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerUniversalTagAttributes();
$this->registerArgument(
'tagName',
'string',
'Tag name to use for enclosing container, list and flags (not finished) only',
false,
'ul'
);
$this->registerArgument(
'tagNameChildren',
'string',
'Tag name to use for child nodes surrounding links, list and flags only',
false,
'li'
);
$this->registerArgument('defaultIsoFlag', 'string', 'ISO code of the default flag');
$this->registerArgument('defaultLanguageLabel', 'string', 'Label for the default language');
$this->registerArgument('order', 'mixed', 'Orders the languageIds after this list', false, '');
$this->registerArgument('labelOverwrite', 'mixed', 'Overrides language labels');
$this->registerArgument(
'hideNotTranslated',
'boolean',
'Hides languageIDs which are not translated',
false,
false
);
$this->registerArgument(
'layout',
'string',
'How to render links when using autorendering. Possible selections: name,flag - use fx "name" or ' .
'"flag,name" or "name,flag"',
false,
'flag,name'
);
$this->registerArgument(
'useCHash',
'boolean',
'Use cHash for typolink. Has no effect on TYPO3 v9.5+',
false,
true
);
$this->registerArgument('flagPath', 'string', 'Overwrites the path to the flag folder', false, '');
$this->registerArgument('flagImageType', 'string', 'Sets type of flag image: png, gif, jpeg', false, 'svg');
$this->registerArgument('linkCurrent', 'boolean', 'Sets flag to link current language or not', false, true);
$this->registerArgument(
'classCurrent',
'string',
'Sets the class, by which the current language will be marked',
false,
'current'
);
$this->registerArgument(
'as',
'string',
'If used, stores the menu pages as an array in a variable named according to this value and renders ' .
'the tag content - which means automatic rendering is disabled if this attribute is used',
false,
'languageMenu'
);
$this->registerArgument('pageUid', 'integer', 'Optional page uid to use.', false, 0);
$this->registerArgument('configuration', 'array', 'Additional typoLink configuration', false, []);
$this->registerArgument('excludeQueryVars', 'string', 'Comma-separate list of variables to exclude', false, '');
$this->registerArgument(
'languages',
'mixed',
'Array, CSV or Traversable containing UIDs of languages to render'
);
}
/**
* Render method
*
* @return string
*/
public function render()
{
if (!is_object($GLOBALS['TSFE']->sys_page)) {
return '';
}
/** @var ContentObjectRenderer|null $contentObject */
$contentObject = ContentObjectFetcher::resolve($this->configurationManager);
if ($contentObject === null) {
throw new Exception('v:page.languageMenu requires a ContentObjectRenderer, none found', 1737807859);
}
$this->cObj = $contentObject;
$this->tagName = is_scalar($this->arguments['tagName']) ? (string) $this->arguments['tagName'] : 'ul';
$this->tag->setTagName($this->tagName);
$this->site = $this->getSite();
$this->defaultLangUid = $this->site->getDefaultLanguage()->getLanguageId();
$this->languageMenu = $this->parseLanguageMenu();
/** @var string $as */
$as = $this->arguments['as'];
$this->renderingContext->getVariableProvider()->add($as, $this->languageMenu);
/** @var string|null $content */
$content = $this->renderChildren();
$content = is_scalar($content) ? (string) $content : '';
$this->renderingContext->getVariableProvider()->remove($as);
if (0 === mb_strlen(trim($content))) {
$content = $this->autoRender();
}
return $content;
}
protected function autoRender(): string
{
$content = $this->getLanguageMenu();
$content = trim($content);
if (!empty($content)) {
$this->tag->setContent($content);
$content = $this->tag->render();
}
return $content;
}
/**
* Get layout 0 (default): list
*/
protected function getLanguageMenu(): string
{
$tagName = $this->arguments['tagNameChildren'];
$html = [];
$itemCount = count($this->languageMenu);
foreach ($this->languageMenu as $index => $var) {
$class = '';
$classes = [];
if ($var['inactive']) {
$classes[] = 'inactive';
}
if ($var['current']) {
$classes[] = $this->arguments['classCurrent'];
}
if (0 === $index) {
$classes[] = 'first';
} elseif (($itemCount - 1) === $index) {
$classes[] = 'last';
}
if (0 < count($classes)) {
$class = ' class="' . implode(' ', $classes) . '" ';
}
if ($var['current'] && !$this->arguments['linkCurrent']) {
$html[] = '<' . $tagName . $class . '>' . $this->getLayout($var) . '</' . $tagName . '>';
} else {
$html[] = '<' . $tagName . $class . '><a href="' . htmlspecialchars($var['url']) . '">' .
$this->getLayout($var) . '</a></' . $tagName . '>';
}
}
return implode(LF, $html);
}
/**
* Returns the flag source given the language ISO code.
*/
protected function getLanguageFlag(string $iso, string $label): string
{
/** @var string $flagPath */
$flagPath = $this->arguments['flagPath'];
/** @var string $flagImageType */
$flagImageType = $this->arguments['flagImageType'];
if ('' !== $flagPath) {
$path = trim($flagPath);
} else {
$path = CoreUtility::getLanguageFlagIconPath();
}
$imgType = trim($flagImageType);
$conf = [
'file' => $path . strtoupper($iso) . '.' . $imgType,
'altText' => $label,
'titleText' => $label
];
if (file_exists(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($conf['file']))) {
$contentObjectDefinition = $this->cObj->getContentObject('IMAGE');
if ($contentObjectDefinition === null) {
return '';
}
return $this->cObj->render($contentObjectDefinition, $conf);
}
return '';
}
/**
* Returns the flag source given a TYPO3 icon identifier.
*/
protected function getLanguageFlagByIdentifier(string $identifier): string
{
/** @var IconFactory $iconFactory */
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$icon = $iconFactory->getIcon($identifier, Icon::SIZE_SMALL);
return $icon->render();
}
/**
* Return the layout: flag & text, flags only or text only.
*/
protected function getLayout(array $language): string
{
/** @var string $layout */
$layout = $this->arguments['layout'];
/** @var string $flagCode */
$flagCode = $language['flagCode'];
$flagImage = false !== stripos($layout, 'flag') ? $flagCode : '';
/** @var string $label */
$label = $language['label'];
switch ($this->arguments['layout']) {
case 'flag':
$html = $flagImage;
break;
case 'name':
$html = $label;
break;
case 'name,flag':
$html = $label;
if ('' !== $flagImage) {
$html .= ' ' . $flagImage;
}
break;
case 'flag,name':
default:
if ('' !== $flagImage) {
$html = $flagImage . ' ' . $label;
} else {
$html = $label;
}
}
return $html;
}
/**
* Sets all parameter for langMenu.
*/
protected function parseLanguageMenu(): array
{
/** @var array $languages */
$languages = $this->arguments['languages'];
/** @var string|null $orderArgument */
$orderArgument = $this->arguments['order'];
/** @var iterable $order */
$order = $orderArgument ? GeneralUtility::trimExplode(',', $orderArgument) : '';
/** @var string $labelOverwrite */
$labelOverwrite = $this->arguments['labelOverwrite'];
if (!empty($labelOverwrite)) {
/** @var array $labelOverwrite */
$labelOverwrite = GeneralUtility::trimExplode(',', $labelOverwrite);
}
// first gather languages into this array so we can reorder it later
$limitLanguages = static::arrayFromArrayOrTraversableOrCSVStatic($languages ?? []);
$limitLanguages = array_filter($limitLanguages);
$tempArray = $this->getLanguagesFromSiteConfiguration($limitLanguages);
// reorder languageMenu
$languageMenu = [];
if (!empty($order)) {
foreach ($order as $value) {
if (isset($tempArray[$value])) {
$languageMenu[$value] = $tempArray[$value];
}
}
} else {
$languageMenu = $tempArray;
}
// overwrite of label
if (!empty($labelOverwrite)) {
$i = 0;
foreach ($languageMenu as $key => $value) {
$languageMenu[$key]['label'] = $labelOverwrite[$i];
$i++;
}
}
// get the languages actually available on this page
$languageUids = $this->getSystemLanguageUids();
if (class_exists(LanguageAspect::class)) {
/** @var Context $context */
$context = GeneralUtility::makeInstance(Context::class);
/** @var LanguageAspect $languageAspect */
$languageAspect = $context->getAspect('language');
$languageUid = $languageAspect->getId();
} else {
$languageUid = $GLOBALS['TSFE']->sys_language_uid;
}
foreach ($languageMenu as $key => $value) {
$current = $languageUid === (integer) $key ? 1 : 0;
$inactive = in_array($key, $languageUids) || (integer) $key === $this->defaultLangUid ? 0 : 1;
$url = $this->getLanguageUrl($key);
if (empty($url)) {
$url = GeneralUtility::getIndpEnv('REQUEST_URI');
}
$languageMenu[$key]['current'] = $current;
$languageMenu[$key]['inactive'] = $inactive;
$languageMenu[$key]['url'] = $url;
$languageMenu[$key]['flagSrc'] = $this->getLanguageFlag($value['flag'] ?? $value['iso'], $value['label']);
// if the user has set a flag path, always use that over the TYPO3 icon factory so the user
// has the option to use custom flag images based on the ISO code of the language.
// if the user has not set a flag path, prefer the TYPO3 icon factory when an icon
// identifier is available (i.e., when using the site-based language lookup) .
if (isset($value['flagIdentifier']) && empty($this->arguments['flagPath'])) {
$languageMenu[$key]['flagCode'] = $this->getLanguageFlagByIdentifier($value['flagIdentifier']);
} else {
$languageMenu[$key]['flagCode'] = $this->getLanguageFlag(
$value['flag'] ?? $value['iso'],
$value['label']
);
}
if ($this->arguments['hideNotTranslated'] && $inactive) {
unset($languageMenu[$key]);
}
}
return $languageMenu;
}
/**
* Get the list of languages from the sys_language table.
*/
protected function getLanguagesFromSysLanguage(array $limitLanguages): array
{
// add default language
$result[0] = [
'label' => $this->arguments['defaultLanguageLabel'] ?? 'English',
'flag' => $this->arguments['defaultIsoFlag'] ?? 'gb'
];
$select = 'uid,title,flag';
$from = 'sys_language';
if (!empty($limitLanguages)) {
$sysLanguage = $GLOBALS['TSFE']->cObj->getRecords(
$from,
['selectFields' => $select, 'pidInList' => 'root', 'uidInList' => implode(',', $limitLanguages)]
);
} else {
$sysLanguage = $GLOBALS['TSFE']->cObj->getRecords(
$from,
['selectFields' => $select, 'pidInList' => 'root']
);
}
foreach ($sysLanguage as $value) {
$result[$value['uid']] = [
'label' => $value['title'],
'flag' => $value['flag'],
];
}
return $result;
}
/**
* Get the list of languages from the site configuration.
*/
protected function getLanguagesFromSiteConfiguration(array $limitLanguages): array
{
$site = $this->getSite();
// get only languages set as visible in frontend
$languages = $site->getLanguages();
$defaultLanguage = $site->getDefaultLanguage();
$result = [];
foreach ($languages as $language) {
if (!empty($limitLanguages) && !in_array($language->getLanguageId(), $limitLanguages)) {
continue;
}
$label = $language->getNavigationTitle();
$flag = $language->getFlagIdentifier();
if ($language->getLanguageId() == $defaultLanguage->getLanguageId()) {
// override label/flag of default language if given
$label = $this->arguments['defaultLanguageLabel'] ?? $label;
$flag = $this->arguments['defaultIsoFlag'] ?? $flag;
}
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '12.4', '>=')) {
$isoCode = $language->getLocale()->getLanguageCode();
} else {
$isoCode = $language->getTwoLetterIsoCode();
}
$result[$language->getLanguageId()] = [
'label' => $label,
'iso' => $isoCode,
'flagIdentifier' => $flag
];
}
return $result;
}
/**
* Get link of language menu entry
*
* @param int|string $languageId
*/
protected function getLanguageUrl($languageId): string
{
/** @var string $excludeVarsArgument */
$excludeVarsArgument = $this->arguments['excludeQueryVars'];
$excludedVars = trim((string) $excludeVarsArgument);
$config = [
'parameter' => $this->getPageUid(),
'returnLast' => 'url',
'additionalParams' => '&L=' . $languageId,
'addQueryString' => 1,
'addQueryString.' => [
'method' => 'GET',
'exclude' => 'id,L,cHash' . ($excludedVars ? ',' . $excludedVars : '')
]
];
if (is_array($this->arguments['configuration'])) {
$config = $this->mergeArrays($config, $this->arguments['configuration']);
}
return $this->cObj->typoLink('', $config);
}
/**
* Get page via pageUid argument or current id
*/
protected function getPageUid(): int
{
/** @var int $pageUid */
$pageUid = $this->arguments['pageUid'];
$pageUid = (integer) $pageUid;
if (0 === $pageUid) {
$pageUid = $GLOBALS['TSFE']->id;
}
return (integer) $pageUid;
}
/**
* Find the site corresponding to the page that the menu is being rendered for
*
* @return Site|\TYPO3\CMS\Core\Site\Entity\Site
*/
protected function getSite()
{
/** @var SiteFinderProxy $siteFinder */
$siteFinder = GeneralUtility::makeInstance(SiteFinderProxy::class);
return $siteFinder->getSiteByPageId($this->getPageUid());
}
/**
* Fetches system languages available on the page depending on the TYPO3 version.
*
* @return int[]
* @phpcsSuppress
* @see https://docs.typo3.org/typo3cms/extensions/core/Changelog/9.0/Important-82445-MigratePagesLanguageOverlayIntoPages.html
*/
protected function getSystemLanguageUids(): array
{
$table = 'pages';
$parentField = 'l10n_parent';
/** @var ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$connection = $connectionPool->getConnectionForTable($table);
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->select('sys_language_uid')
->from($table)
->where(
$queryBuilder->expr()->eq($parentField, $this->getPageUid())
);
$result = DoctrineQueryProxy::executeQueryOnQueryBuilder($queryBuilder);
$rows = DoctrineQueryProxy::fetchAllAssociative($result);
return array_column($rows, 'sys_language_uid');
}
}