+ HTML;
}
}
diff --git a/src/Twig/Extension/TwigLanguageSwitcher.php b/src/Twig/Extension/TwigLanguageSwitcher.php
index aa9b213..3e10401 100644
--- a/src/Twig/Extension/TwigLanguageSwitcher.php
+++ b/src/Twig/Extension/TwigLanguageSwitcher.php
@@ -1,100 +1,112 @@
documentService = $documentService;
+ $this->logger = $logger;
}
/**
- * @inheritDoc
+ * Registers Twig functions provided by this extension.
+ *
+ * @return array An array of TwigFunction instances.
*/
- public function getFunctions(): array
+ final public function getFunctions(): array
{
return [
- new TwigFunction('twigExtensionLocalizedLinks',
- [$this, 'getLocalizedLinks']),
- new TwigFunction('twigExtensionLanguageFlag',
- [$this, 'getLanguageFlag'])
+ new TwigFunction('twigExtensionLocalizedLinks', [$this, 'getLocalizedLinks']),
+ new TwigFunction('twigExtensionLanguageFlag', [$this, 'getLanguageFlag'])
];
}
- public function getLocalizedLinks(Document $document): array
+ /**
+ * Fetches localized links for a given document.
+ *
+ * @param Document $document The document to fetch localized links for.
+ * @return array An array of localized links, with language codes as keys and details as values.
+ */
+ final public function getLocalizedLinks(Document $document): array
{
- $translations = $this->documentService->getTranslations($document);
+ try {
+ $translations = $this->documentService->getTranslations($document);
+ $validLanguages = Tool::getValidLanguages();
+ $links = [];
- $links = [];
- foreach (Tool::getValidLanguages() as $language) {
- $target = trim('/' . $language);
+ foreach ($validLanguages as $language) {
+ $target = '/' . $language;
- //skip if root document for local is missing
- if (!(Document::getByPath($target) instanceof Document)) {
- continue;
- }
+ if (!(Document::getByPath($target) instanceof Document)) {
+ continue;
+ }
- if (isset($translations[$language])) {
- $localizedDocument = Document::getById($translations[$language]);
- if ($localizedDocument) {
- $target = $localizedDocument->getFullPath();
+ if (isset($translations[$language])) {
+ $localizedDocument = Document::getById($translations[$language]);
+ if ($localizedDocument) {
+ $target = $localizedDocument->getFullPath();
+ }
}
+
+ $links[$language] = [
+ 'link' => $target,
+ 'text' => \Locale::getDisplayLanguage($language)
+ ];
}
- $links[$language] = [
- 'link' => $target,
- 'text' => \Locale::getDisplayLanguage($language)
- ];
+ return $links;
+ } catch (\Exception $e) {
+ $this->logger->error('Error fetching localized links: ' . $e->getMessage());
+ return [];
}
-
- return $links;
}
/**
- * @param string $language
- * @return string
+ * Retrieves the web path to a flag icon for the specified language.
+ *
+ * @param string $language The language code.
+ * @return string The web path to the language's flag icon.
*/
- public function getLanguageFlag(string $language): string
+ final public function getLanguageFlag(string $language): string
{
$flag = '';
- if (Tool::isValidLanguage($language)) {
- $flag = self::getLanguageFlagFile($language);
+ try {
+ if (Tool::isValidLanguage($language)) {
+ $flag = self::getLanguageFlagFile($language);
+ }
+ $flag = preg_replace('@^' . preg_quote(PIMCORE_WEB_ROOT, '@') . '@', '', $flag);
+ } catch (\Exception $e) {
+ $this->logger->error('Error fetching language flag: ' . $e->getMessage());
}
- $flag = preg_replace('@^' . preg_quote(PIMCORE_WEB_ROOT, '@') . '@', '', $flag);
return $flag;
}
/**
- * @param string $language
+ * Determines the file path for a given language's flag icon.
*
- * @return string
+ * @param string $language The language code.
+ * @return string The file path for the flag icon.
*/
public static function getLanguageFlagFile(string $language): string
{
@@ -102,7 +114,23 @@ public static function getLanguageFlagFile(string $language): string
$code = strtolower($language);
$iconPath = $basePath . '/countries/_unknown.svg';
- $languageCountryMapping = [
+ $languageCountryMapping = self::getLanguageCountryMapping();
+
+ if (array_key_exists($code, $languageCountryMapping)) {
+ $iconPath = $basePath . '/countries/' . $languageCountryMapping[$code] . '.svg';
+ }
+
+ return $iconPath;
+ }
+
+ /**
+ * Returns a mapping of language codes to their corresponding country code for flag icons.
+ *
+ * @return array The mapping of language codes to country codes.
+ */
+ private static function getLanguageCountryMapping(): array
+ {
+ return [
'aa' => 'er', 'af' => 'za', 'am' => 'et', 'as' => 'in', 'ast' => 'es', 'asa' => 'tz',
'az' => 'az', 'bas' => 'cm', 'eu' => 'es', 'be' => 'by', 'bem' => 'zm', 'bez' => 'tz', 'bg' => 'bg',
'bm' => 'ml', 'bn' => 'bd', 'br' => 'fr', 'brx' => 'in', 'bs' => 'ba', 'cs' => 'cz', 'da' => 'dk',
@@ -118,13 +146,7 @@ public static function getLanguageFlagFile(string $language): string
'cy' => 'gb-wls', 'cy-gb' => 'gb-wls', 'fy' => 'nl', 'xh' => 'za', 'yo' => 'bj', 'zu' => 'za',
'ta' => 'lk', 'te' => 'in', 'ss' => 'za', 'sw' => 'ke', 'so' => 'so', 'si' => 'lk', 'ii' => 'cn',
'zh-hans' => 'cn', 'sn' => 'zw', 'rm' => 'ch', 'pa' => 'in', 'fa' => 'ir', 'lv' => 'lv', 'gl' => 'es',
- 'fil' => 'ph',
+ 'fil' => 'ph'
];
-
- if (array_key_exists($code, $languageCountryMapping)) {
- $iconPath = $basePath . '/countries/' . $languageCountryMapping[$code] . '.svg';
- }
-
- return $iconPath;
}
}
diff --git a/src/Twig/Extension/TwigQrCode.php b/src/Twig/Extension/TwigQrCode.php
index 5cf8ec2..0b7a362 100644
--- a/src/Twig/Extension/TwigQrCode.php
+++ b/src/Twig/Extension/TwigQrCode.php
@@ -23,39 +23,40 @@ public function __construct(Translator $translator)
$this->translator = $translator;
}
- public function getFunctions(): array
+ final public function getFunctions(): array
{
return [
- new TwigFunction('twigExtensionQrImage',
- [$this, 'getQrImg'], ['is_safe' => ['html']])
+ new TwigFunction(
+ 'twigExtensionQrImage',
+ [$this, 'getQrImg'],
+ ['is_safe' => ['html']]
+ )
];
}
- public function getQrImg(
+ final public function getQrImg(
string $qrData,
int $size = 150,
int $margin = 0,
array $qrColor = [0, 0, 0],
array $bgColor = [255, 225, 255]
- ): string
- {
+ ): string {
- $qrImg = [];
- $qrImg[] .= '