Skip to content

Commit

Permalink
Throw 404 when visited entity is not in current language
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinHoutevelts committed Oct 25, 2019
1 parent 514b782 commit b995bce
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
31 changes: 29 additions & 2 deletions src/Controller/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,34 @@ class FrontController extends ControllerBase
/** @var Dispatcher */
protected $dispatcher;

/** @var array */
protected $settings;

protected $throw404WhenNotTranslated = true;

public function __construct(
ArgumentResolverInterface $argumentResolver,
ControllerResolverInterface $controllerResolver,
Dispatcher $dispatcher
Dispatcher $dispatcher,
array $settings
) {
$this->argumentResolver = $argumentResolver;
$this->dispatcher = $dispatcher;
$this->controllerResolver = $controllerResolver;
$this->settings = $settings;

if (isset($this->settings['404_when_not_translated'])) {
$this->throw404WhenNotTranslated = $this->settings['404_when_not_translated'];
}
}

public static function create(ContainerInterface $container)
{
return new static(
$container->get('http_kernel.controller.argument_resolver'),
$container->get('controller_resolver'),
$container->get('wmcontroller.cache.dispatcher')
$container->get('wmcontroller.cache.dispatcher'),
$container->getParameter('wmcontroller.settings'),
);
}

Expand All @@ -60,6 +72,7 @@ public function node(Request $request, EntityInterface $node)
*/
protected function forward(Request $request, EntityInterface $entity)
{
$this->validateLangcode($entity);
$this->request = $request;

$controller = [$this->getController($entity), 'show'];
Expand Down Expand Up @@ -117,5 +130,19 @@ protected function camelize($input, $separator = '_')
{
return str_replace($separator, '', ucwords($input, $separator));
}

protected function validateLangcode(EntityInterface $entity)
{
$langcode = $this->languageManager()->getCurrentLanguage()->getId();
$isMultiLang = count($this->languageManager()->getLanguages()) > 1;

if (
$isMultiLang
&& $this->throw404WhenNotTranslated
&& $entity->language()->getId() !== $langcode
) {
throw new NotFoundHttpException();
}
}
}

11 changes: 9 additions & 2 deletions src/EventSubscriber/InjectFrontControllerRouteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\wmcontroller\EventSubscriber;

use Drupal\wmcontroller\Controller\FrontController;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Drupal\Core\Routing\RoutingEvents;
Expand All @@ -15,6 +16,8 @@ class InjectFrontControllerRouteSubscriber extends RouteSubscriberBase
{
protected $settings;

protected $frontController = FrontController::class;

public function __construct(array $settings)
{
if (empty($settings['module'])) {
Expand All @@ -23,6 +26,10 @@ public function __construct(array $settings)
);
}

if (isset($settings['frontcontroller'])) {
$this->frontController = $settings['frontcontroller'];
}

$this->settings = $settings;
}

Expand Down Expand Up @@ -69,8 +76,8 @@ protected function alterRoute(Route $route, $controllerMethod)
// The FrontController will delegate to a bundle-specific controller
$defaults['_controller'] = sprintf(
'%s%s%s',
$this->settings['frontcontroller'],
class_exists($this->settings['frontcontroller'])
$this->frontController,
class_exists($this->frontController)
? '::' // FQN::method
: ':', // servicename:method
$controllerMethod
Expand Down
5 changes: 5 additions & 0 deletions wmcontroller.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ parameters:
# The controller responsible for forwarding to bundle-specific controllers
frontcontroller: 'Drupal\wmcontroller\Controller\FrontController'

# Throw a 404 NotFoundHttpException when an entity is not translated
# in the current language. ( /en/node/123 gives 404 if node/123 has no
# en translation )
404_when_not_translated: true

# Expiry rules.
# maxage = client side caching duration
# s-maxage = server side caching duration (this can be drupal db or a cdn)
Expand Down

0 comments on commit b995bce

Please sign in to comment.