-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add setting to prioritize root page for countries
- Loading branch information
Showing
5 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
use Contao\CoreBundle\DataContainer\PaletteManipulator; | ||
use Contao\System; | ||
|
||
PaletteManipulator::create() | ||
->addField('geoip_priority', 'language_legend', PaletteManipulator::POSITION_APPEND) | ||
->applyToPalette('root', 'tl_page') | ||
->applyToPalette('rootfallback', 'tl_page') | ||
; | ||
|
||
$GLOBALS['TL_DCA']['tl_page']['fields'] += [ | ||
'geoip_priority' => [ | ||
'exclude' => true, | ||
'inputType' => 'select', | ||
'options_callback' => static fn () => System::getContainer()->get('contao.intl.countries')->getCountries(), | ||
'eval' => [ | ||
'includeBlankOption' => true, | ||
'multiple' => true, | ||
'chosen' => true, | ||
'csv' => ',', | ||
'tl_class' => 'clr w50', | ||
], | ||
'sql' => ['type' => 'string', 'length' => 255, 'default' => ''], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<xliff version="1.1"> | ||
<file source-language="en"> | ||
<body> | ||
<trans-unit id="tl_page.geoip_priority.0"> | ||
<source>GeoIP Priority</source> | ||
</trans-unit> | ||
<trans-unit id="tl_page.geoip_priority.1"> | ||
<source>Select countries where this root page should have priority regardless of browser language detection.</source> | ||
</trans-unit> | ||
</body> | ||
</file> | ||
</xliff> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Terminal42\Geoip2CountryBundle\Routing; | ||
|
||
use Contao\PageModel; | ||
use Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Terminal42\Geoip2CountryBundle\CountryProvider; | ||
|
||
class CountryPriorityFilter implements RouteFilterInterface | ||
{ | ||
public function __construct(private readonly CountryProvider $countryProvider) | ||
{ | ||
} | ||
|
||
public function filter(RouteCollection $collection, Request $request): RouteCollection | ||
{ | ||
$hasPriority = false; | ||
$country = $this->countryProvider->getCountryCode($request); | ||
$map = []; | ||
|
||
foreach ($collection as $name => $route) { | ||
$pageModel = $route->getDefault('pageModel'); | ||
|
||
if ( | ||
!$pageModel instanceof PageModel | ||
&& !str_ends_with($name, '.root') | ||
&& !str_ends_with($name, '.fallback') | ||
) { | ||
continue; | ||
} | ||
|
||
$map[$name] = $this->hasPriority($this->getRootPage($pageModel), $country); | ||
$hasPriority = $hasPriority ?: $map[$name]; | ||
} | ||
|
||
if ($hasPriority) { | ||
foreach ($map as $name => $priority) { | ||
if (!$priority) { | ||
$collection->remove($name); | ||
continue; | ||
} | ||
|
||
// Fake page to be fallback to fix Contao\CoreBundle\Routing\Matcher\LanguageFilter | ||
$collection->get($name)->getDefault('pageModel')->rootIsFallback = true; | ||
} | ||
} | ||
|
||
return $collection; | ||
} | ||
|
||
private function getRootPage(PageModel $pageModel): PageModel | ||
{ | ||
if ('root' === $pageModel->type) { | ||
return $pageModel; | ||
} | ||
|
||
return PageModel::findById($pageModel->loadDetails()->rootId); | ||
} | ||
|
||
private function hasPriority(PageModel $pageModel, string $country): bool | ||
{ | ||
return \in_array($country, explode(',', (string) $pageModel->geoip_priority), true); | ||
} | ||
} |