Merge branch 'bugfix/dont-show-pages-in-missing-locales'

This commit is contained in:
Bob den Otter
2020-10-28 15:46:45 +01:00
3 changed files with 40 additions and 2 deletions
@@ -41,6 +41,12 @@ class ListingController extends TwigAwareController implements FrontendZoneInter
public function listing(ContentRepository $contentRepository, string $contentTypeSlug): Response
{
$contentType = ContentType::factory($contentTypeSlug, $this->config->get('contenttypes'));
// If the locale is the wrong locale
if (! $this->validLocaleForContentType($contentType)) {
return $this->redirectToDefaultLocale();
}
$page = (int) $this->getFromRequest('page', '1');
$amountPerPage = $contentType->get('listing_records');
$order = $this->getFromRequest('order', $contentType->get('order'));
+33 -1
View File
@@ -6,6 +6,7 @@ namespace Bolt\Controller;
use Bolt\Canonical;
use Bolt\Configuration\Config;
use Bolt\Configuration\Content\ContentType;
use Bolt\Entity\Content;
use Bolt\Entity\Field\TemplateselectField;
use Bolt\Enum\Statuses;
@@ -47,10 +48,13 @@ class TwigAwareController extends AbstractController
/** @var TemplateChooser */
protected $templateChooser;
/** @var string */
private $defaultLocale;
/**
* @required
*/
public function setAutowire(Config $config, Environment $twig, Packages $packages, Canonical $canonical, Sanitiser $sanitiser, RequestStack $requestStack, TemplateChooser $templateChooser): void
public function setAutowire(Config $config, Environment $twig, Packages $packages, Canonical $canonical, Sanitiser $sanitiser, RequestStack $requestStack, TemplateChooser $templateChooser, string $defaultLocale): void
{
$this->config = $config;
$this->twig = $twig;
@@ -59,6 +63,7 @@ class TwigAwareController extends AbstractController
$this->sanitiser = $sanitiser;
$this->request = $requestStack->getCurrentRequest();
$this->templateChooser = $templateChooser;
$this->defaultLocale = $defaultLocale;
}
/**
@@ -133,6 +138,11 @@ class TwigAwareController extends AbstractController
throw new NotFoundHttpException('Content is not viewable');
}
// If the locale is the wrong locale
if (! $this->validLocaleForContentType($record->getDefinition())) {
return $this->redirectToDefaultLocale();
}
$singularSlug = $record->getContentTypeSingularSlug();
$context = [
@@ -151,6 +161,28 @@ class TwigAwareController extends AbstractController
return $this->render($templates, $context);
}
protected function validLocaleForContentType(ContentType $contentType): bool
{
if ($contentType->isKeyNotEmpty('locales')) {
return $contentType->get('locales')->contains($this->request->getLocale());
}
return $this->request->getLocale() === $this->defaultLocale;
}
protected function redirectToDefaultLocale(): ?Response
{
$this->request->getSession()->set('_locale', $this->defaultLocale);
$params = $this->request->attributes->get('_route_params');
if (isset($params['_locale'])) {
$params['_locale'] = $this->defaultLocale;
}
return $this->redirectToRoute($this->request->get('_route'), $params);
}
private function setTwigLoader(): void
{
/** @var FilesystemLoader $twigLoaders */
+1 -1
View File
@@ -39,7 +39,7 @@ Feature: Edit record
And I wait for ".admin__header--title"
Then the "field-title" field should contain "Changed title EN"
When I am on "/page/1?locale=nl"
When I am on "/page/1?_locale=nl"
Then I should see "Changed title NL"
@javascript