Skip to content

Commit cec2e1e

Browse files
committed
refactor: removed deprecated renderChangeLanguageSelector() method
1 parent 160c323 commit cec2e1e

File tree

6 files changed

+83
-44
lines changed

6 files changed

+83
-44
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{#
2+
# Twig macros for FAQ rendering
3+
#
4+
# This Source Code Form is subject to the terms of the Mozilla Public License,
5+
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
6+
# obtain one at https://mozilla.org/MPL/2.0/.
7+
#
8+
# @package phpMyFAQ
9+
# @author Thorsten Rinne <[email protected]>
10+
# @copyright 2026 phpMyFAQ Team
11+
# @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
12+
# @link https://www.phpmyfaq.de
13+
# @since 2026-01-05
14+
#}
15+
16+
{#
17+
# Renders a select box with all translations of a FAQ
18+
#
19+
# @param languageUrls object Object with language codes as keys and URLs as values
20+
# @param currentLang string The current FAQ language
21+
#}
22+
{% macro renderChangeLanguageSelector(languageUrls, currentLang) %}
23+
{% if languageUrls|length > 1 %}
24+
<form method="post">
25+
<select class="form-select" name="language" onchange="top.location.href = this.options[this.selectedIndex].value;">
26+
{% for language, url in languageUrls %}
27+
<option value="{{ url }}"{% if currentLang == language %} selected{% endif %}>
28+
{{ language|getFromLanguageCode }}
29+
</option>
30+
{% endfor %}
31+
</select>
32+
</form>
33+
{% endif %}
34+
{% endmacro %}
35+
36+
37+

phpmyfaq/assets/templates/default/faq.twig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% extends 'index.twig' %}
22
{% import 'core/comment.macros.twig' as commentMacros %}
3+
{% import 'core/faq.macros.twig' as faqMacros %}
34

45
{% block richSnippets %}
56
<script type="application/ld+json">
@@ -106,7 +107,9 @@
106107
</li>
107108
{% endif %}
108109
{% if msgChangeLanguage is defined %}
109-
<li class="list-group-item bg-transparent">{{ switchLanguage|raw }}</li>
110+
<li class="list-group-item bg-transparent">
111+
{{ faqMacros.renderChangeLanguageSelector(languageUrls, currentLanguage) }}
112+
</li>
110113
{% endif %}
111114
{% if permissionEditFaq == true %}
112115
<li class="list-group-item bg-transparent">
@@ -183,7 +186,7 @@
183186
<div class="modal-dialog modal-lg">
184187
<div class="modal-content">
185188
<div class="modal-header">
186-
<h1 class="modal-title fs-5" id="commentModalLabel">{{ msgCommentHeader }}</h1>
189+
<h1 class="modal-title fs-5" id="commentModalLabel">{{ 'msgCommentHeader' | translate }}</h1>
187190
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{{ msgCancel }}"></button>
188191
</div>
189192
<div class="modal-body">

phpmyfaq/src/phpMyFAQ/Controller/Administration/StatisticsSearchController.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@
2424
use phpMyFAQ\Pagination;
2525
use phpMyFAQ\Session\Token;
2626
use phpMyFAQ\Translation;
27-
use phpMyFAQ\Twig\Extensions\LanguageCodeTwigExtension;
2827
use Symfony\Component\HttpFoundation\Request;
2928
use Symfony\Component\HttpFoundation\Response;
3029
use Symfony\Component\Routing\Attribute\Route;
3130
use Twig\Error\LoaderError;
32-
use Twig\Extension\AttributeExtension;
3331

3432
final class StatisticsSearchController extends AbstractAdministrationController
3533
{
@@ -59,7 +57,6 @@ public function index(Request $request): Response
5957
];
6058
$pagination = new Pagination($options);
6159

62-
$this->addExtension(new AttributeExtension(LanguageCodeTwigExtension::class));
6360
return $this->render('@admin/statistics/search.twig', [
6461
...$this->getHeader($request),
6562
...$this->getFooter(),

phpmyfaq/src/phpMyFAQ/Controller/Administration/SystemInformationController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function index(Request $request): Response
6969
$openSearchInformation = 'n/a';
7070
}
7171

72-
$translationInformation = new TranslationStatistics($this->configuration);
72+
$translationInformation = new TranslationStatistics();
7373
$translationStatistics = $translationInformation->getStatistics();
7474

7575
$this->addExtension(new AttributeExtension(LanguageCodeTwigExtension::class));

phpmyfaq/src/phpMyFAQ/Controller/Frontend/FaqController.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,22 @@
2828
use phpMyFAQ\Faq\FaqCreationService;
2929
use phpMyFAQ\Faq\FaqDisplayService;
3030
use phpMyFAQ\Filter;
31+
use phpMyFAQ\Language;
32+
use phpMyFAQ\Link;
3133
use phpMyFAQ\Seo;
3234
use phpMyFAQ\Services;
3335
use phpMyFAQ\Session\Token;
3436
use phpMyFAQ\Strings;
3537
use phpMyFAQ\Translation;
38+
use phpMyFAQ\Twig\Extensions\LanguageCodeTwigExtension;
3639
use phpMyFAQ\Utils;
3740
use phpMyFAQ\Visits;
3841
use Symfony\Component\HttpFoundation\RedirectResponse;
3942
use Symfony\Component\HttpFoundation\Request;
4043
use Symfony\Component\HttpFoundation\Response;
4144
use Symfony\Component\Routing\Attribute\Route;
4245
use Twig\Error\LoaderError;
46+
use Twig\Extension\AttributeExtension;
4347
use Twig\TwigFilter;
4448

4549
final class FaqController extends AbstractFrontController
@@ -134,6 +138,7 @@ public function add(Request $request): Response
134138
$templateVars[$required] = (int) $input->input_required !== 0 ? 'required' : '';
135139
}
136140

141+
$this->addExtension(new AttributeExtension(LanguageCodeTwigExtension::class));
137142
return $this->render('add.twig', $templateVars);
138143
}
139144

@@ -171,7 +176,8 @@ public function solution(Request $request): Response
171176
/**
172177
* Displays a single FAQ article with comments, ratings, and related content
173178
*
174-
* @throws Exception|LoaderError*@throws \Exception
179+
* @throws Exception|LoaderError|\Exception
180+
*
175181
*
176182
*/
177183
#[Route(path: '/faq/{categoryId}/{faqId}/{slug}.html', name: 'public.faq.show', methods: ['GET'])]
@@ -189,6 +195,21 @@ public function show(Request $request): Response
189195
$faqId = Filter::filterVar($request->query->get('id'), FILTER_VALIDATE_INT, 0);
190196
}
191197

198+
// Get language from route parameter (for /content/ URLs) or query parameter (for legacy URLs)
199+
$requestedLanguage =
200+
$request->attributes->get('language') ?? $request->query->get('artlang') ?? $this->configuration
201+
->getLanguage()
202+
->getLanguage();
203+
204+
// Temporarily set the language in session for this request
205+
$session = $this->container->get('session');
206+
$originalLanguage = $session->get('lang');
207+
if ($requestedLanguage !== $originalLanguage) {
208+
$session->set('lang', $requestedLanguage);
209+
// Update the static language variable
210+
Language::$language = $requestedLanguage;
211+
}
212+
192213
$solutionId = Filter::filterVar($request->query->get('solution_id'), FILTER_VALIDATE_INT);
193214
$highlight = Filter::filterVar($request->query->get('highlight'), FILTER_SANITIZE_SPECIAL_CHARS);
194215
$bookmarkAction = Filter::filterVar($request->query->get('bookmark_action'), FILTER_SANITIZE_SPECIAL_CHARS);
@@ -244,6 +265,21 @@ public function show(Request $request): Response
244265
$availableLanguages = $faqDisplayService->getAvailableLanguages($faq->faqRecord['id']);
245266
$tagsHtml = $faqDisplayService->getTagsHtml($faqId);
246267

268+
// Generate language URLs with SEO slugs
269+
$languageUrls = [];
270+
foreach ($availableLanguages as $language) {
271+
$url = sprintf(
272+
'%sindex.php?action=faq&cat=%d&id=%d&artlang=%s',
273+
$this->configuration->getDefaultUrl(),
274+
$cat,
275+
$faqId,
276+
$language,
277+
);
278+
$link = new Link($url, $this->configuration);
279+
$link->setTitle($question);
280+
$languageUrls[$language] = $link->toString();
281+
}
282+
247283
// Comment permissions
248284
$expired = $faqDisplayService->isExpired();
249285

@@ -313,14 +349,14 @@ public function show(Request $request): Response
313349
'linkToPdf' => $faqServices->getPdfLink(),
314350
'msgAverageVote' => Translation::get(key: 'msgAverageVote'),
315351
'renderVotingResult' => $faqDisplayService->getRating($faqId),
316-
'switchLanguage' => $faqDisplayService->getFaqHelper()->renderChangeLanguageSelector($faq, $cat),
352+
'languageUrls' => $languageUrls,
353+
'currentLanguage' => $faq->faqRecord['lang'],
317354
'msgVoteBad' => Translation::get(key: 'msgVoteBad'),
318355
'msgVoteGood' => Translation::get(key: 'msgVoteGood'),
319356
'msgVoteSubmit' => Translation::get(key: 'msgVoteSubmit'),
320357
'msgWriteComment' => Translation::get(key: 'msgWriteComment'),
321358
'id' => $faqId,
322359
'lang' => $this->configuration->getLanguage()->getLanguage(),
323-
'msgCommentHeader' => Translation::get(key: 'msgCommentHeader'),
324360
'msgNewContentName' => Translation::get(key: 'msgNewContentName'),
325361
'msgNewContentMail' => Translation::get(key: 'msgNewContentMail'),
326362
'defaultContentMail' => $this->currentUser->getUserId() > 0
@@ -401,6 +437,7 @@ public function show(Request $request): Response
401437
$templateVars['renderRelatedArticles'] = $relatedFaqs;
402438
}
403439

440+
$this->addExtension(new AttributeExtension(LanguageCodeTwigExtension::class));
404441
return $this->render('faq.twig', $templateVars);
405442
}
406443

phpmyfaq/src/phpMyFAQ/Helper/FaqHelper.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,39 +58,6 @@ public function rewriteUrlFragments(string $answer, string $currentUrl): string
5858
return str_replace('href="#', 'href="' . $currentUrl . '#', $answer);
5959
}
6060

61-
/**
62-
* Renders a select box with all translations of a FAQ.
63-
*
64-
* @todo This method should be moved to a Twig macro.
65-
*/
66-
#[\Deprecated(message: 'Rewrite this method to use Twig, will be removed in v4.2')]
67-
public function renderChangeLanguageSelector(Faq $faq, int $categoryId): string
68-
{
69-
$html = '';
70-
$faqUrl = sprintf('?action=faq&cat=%d&id=%d&artlang=%%s', $categoryId, $faq->faqRecord['id']);
71-
72-
$oLink = new Link($this->configuration->getDefaultUrl() . $faqUrl, $this->configuration);
73-
$oLink->setTitle($faq->faqRecord['title']);
74-
75-
$availableLanguages = $this->configuration->getLanguage()->isLanguageAvailable((int) $faq->faqRecord['id']);
76-
77-
if ((is_countable($availableLanguages) ? count($availableLanguages) : 0) > 1) {
78-
$html = '<form method="post">';
79-
$html .= '<select class="form-select" name="language" ';
80-
$html .= 'onchange="top.location.href = this.options[this.selectedIndex].value;">';
81-
82-
foreach ($availableLanguages as $availableLanguage) {
83-
$html .= sprintf('<option value="%s"', sprintf($oLink->toString(), $availableLanguage));
84-
$html .= $faq->faqRecord['lang'] === $availableLanguage ? ' selected' : '';
85-
$html .= sprintf('>%s</option>', LanguageCodes::get($availableLanguage));
86-
}
87-
88-
$html .= '</select></form>';
89-
}
90-
91-
return $html;
92-
}
93-
9461
/**
9562
* Renders a preview of the answer
9663
*
@@ -125,10 +92,8 @@ public function renderAnswerPreview(string $answer, int $wordCount): string
12592
*/
12693
public function createOverview(Category $category, Faq $faq, string $language = ''): array
12794
{
128-
// Initialize categories
12995
$category->transform(0);
13096

131-
// Get all FAQs
13297
$faq->getAllFaqs(FAQ_SORTING_TYPE_CATID_FAQID, ['lang' => $language, 'active' => 'yes']);
13398

13499
return $faq->faqRecords;

0 commit comments

Comments
 (0)