|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace eZ\Bundle\EzPublishCoreBundle\SiteAccess; |
|
8
|
|
|
|
|
9
|
|
|
use eZ\Publish\API\Repository\LanguageResolver as APILanguageResolver; |
|
10
|
|
|
use eZ\Publish\API\Repository\Values\Content\Language; |
|
11
|
|
|
use eZ\Publish\Core\MVC\ConfigResolverInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Resolves language settings for use in SiteAccess aware Repository. |
|
15
|
|
|
*/ |
|
16
|
|
|
class LanguageResolver implements APILanguageResolver |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var bool |
|
20
|
|
|
*/ |
|
21
|
|
|
private $defaultUseAlwaysAvailable; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var bool |
|
25
|
|
|
*/ |
|
26
|
|
|
private $defaultShowAllTranslations; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Values typically provided by user context, will need to be set depending on your own custom logic using setter. |
|
30
|
|
|
* |
|
31
|
|
|
* E.g. Backend UI might expose a language selector for the whole backend that should be reflected on both |
|
32
|
|
|
* UI strings as well as default languages to prioritize for repository objects. |
|
33
|
|
|
* |
|
34
|
|
|
* If set, this will have priority over configured languages. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string|null |
|
37
|
|
|
*/ |
|
38
|
|
|
private $contextLanguage; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var \eZ\Publish\Core\MVC\ConfigResolverInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $configResolver; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct( |
|
46
|
|
|
ConfigResolverInterface $configResolver, |
|
47
|
|
|
bool $defaultUseAlwaysAvailable = true, |
|
48
|
|
|
bool $defaultShowAllTranslations = false |
|
49
|
|
|
) { |
|
50
|
|
|
$this->configResolver = $configResolver; |
|
51
|
|
|
$this->defaultUseAlwaysAvailable = $defaultUseAlwaysAvailable; |
|
52
|
|
|
$this->defaultShowAllTranslations = $defaultShowAllTranslations; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function setContextLanguage(?string $contextLanguage): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->contextLanguage = $contextLanguage; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getPrioritizedLanguages(?array $forcedLanguages): array |
|
61
|
|
|
{ |
|
62
|
|
|
// Skip if languages param has been set by API user |
|
63
|
|
|
if ($forcedLanguages !== null) { |
|
64
|
|
|
return $forcedLanguages; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Detect if we should load all languages by default |
|
68
|
|
|
if ($this->defaultShowAllTranslations) { |
|
69
|
|
|
return Language::ALL; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// create language based on context and configuration, where context language is made most important one |
|
73
|
|
|
$languages = []; |
|
74
|
|
|
if ($this->contextLanguage !== null) { |
|
75
|
|
|
$languages[] = $this->contextLanguage; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return array_values(array_unique(array_merge($languages, $this->resolveConfigLanguages()))); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function setDefaultUseAlwaysAvailable(bool $defaultUseAlwaysAvailable): void |
|
82
|
|
|
{ |
|
83
|
|
|
$this->defaultUseAlwaysAvailable = $defaultUseAlwaysAvailable; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getUseAlwaysAvailable(?bool $forcedUseAlwaysAvailable = null): bool |
|
87
|
|
|
{ |
|
88
|
|
|
if ($forcedUseAlwaysAvailable !== null) { |
|
89
|
|
|
return $forcedUseAlwaysAvailable; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this->defaultUseAlwaysAvailable; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function setShowAllTranslations(bool $defaultShowAllTranslations): void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->defaultShowAllTranslations = $defaultShowAllTranslations; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function getShowAllTranslations(?bool $forcedShowAllTranslations = null): bool |
|
101
|
|
|
{ |
|
102
|
|
|
if ($forcedShowAllTranslations !== null) { |
|
103
|
|
|
return $forcedShowAllTranslations; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this->defaultShowAllTranslations; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Resolve current config languages defined via scope/SiteAccess context. |
|
111
|
|
|
* |
|
112
|
|
|
* @return string[] |
|
113
|
|
|
*/ |
|
114
|
|
|
private function resolveConfigLanguages(): array |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->configResolver->getParameter('languages'); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|