1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Twig\Extension\AbstractExtension; |
4
|
|
|
use Twig\TwigFilter; |
5
|
|
|
|
6
|
|
|
class GlobalUrlExtension extends AbstractExtension |
7
|
|
|
{ |
8
|
|
|
public function getFilters() |
9
|
|
|
{ |
10
|
|
|
return [ |
11
|
|
|
new TwigFilter('global_url', [$this, 'globalUrlFilter']), |
12
|
|
|
]; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Creates Skosmos links for top-level pages |
17
|
|
|
* @param string $page 'home', 'about', or 'feedback' |
18
|
|
|
* @param string $lang language code (e.g., 'en', 'fi') |
19
|
|
|
* @param string $contentLang content language code (e.g., 'en', 'fi') |
20
|
|
|
* @param string $vocabid vocabulary ID (optional) |
21
|
|
|
* @param bool $anylang whether anylang parameter is active |
22
|
|
|
* @return string containing the Skosmos link |
23
|
|
|
*/ |
24
|
|
|
public function globalUrlFilter($page, $lang, $contentLang = null, $vocabid = null, $anylang = false) |
25
|
|
|
{ |
26
|
|
|
$url = ''; |
27
|
|
|
|
28
|
|
|
// Build base URL |
29
|
|
|
if ($vocabid && $vocabid !== '') { |
30
|
|
|
$url = $vocabid . '/'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$url .= $lang; |
34
|
|
|
|
35
|
|
|
// Add page suffix for non-home pages |
36
|
|
|
if ($page !== 'home') { |
37
|
|
|
$url .= '/' . $page; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Build query parameters |
41
|
|
|
$params = []; |
42
|
|
|
|
43
|
|
|
// Add clang parameter if needed |
44
|
|
|
if ($contentLang && $contentLang !== $lang) { |
45
|
|
|
$params['clang'] = $contentLang; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Add anylang parameter if needed |
49
|
|
|
if ($anylang) { |
50
|
|
|
$params['anylang'] = 'on'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Append parameters if any |
54
|
|
|
if (!empty($params)) { |
55
|
|
|
$url .= '?' . http_build_query($params); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $url; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|