diff --git a/globalredirect.services.yml b/globalredirect.services.yml index a46ff53..846eb2a 100644 --- a/globalredirect.services.yml +++ b/globalredirect.services.yml @@ -1,7 +1,7 @@ services: globalredirect.subscriber: class: Drupal\globalredirect\EventSubscriber\GlobalredirectSubscriber - arguments: ['@config.factory', '@path.alias_manager', '@language_manager', '@module_handler', '@entity.manager', '@globalredirect.checker', '@router.request_context'] + arguments: ['@config.factory', '@path.alias_manager', '@language_manager', '@module_handler', '@entity.manager', '@globalredirect.checker', '@router.request_context', '@url_generator'] tags: - { name: event_subscriber } globalredirect.checker: diff --git a/src/EventSubscriber/GlobalredirectSubscriber.php b/src/EventSubscriber/GlobalredirectSubscriber.php index 4e1b136..ffc1822 100644 --- a/src/EventSubscriber/GlobalredirectSubscriber.php +++ b/src/EventSubscriber/GlobalredirectSubscriber.php @@ -13,8 +13,10 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Path\AliasManager; use Drupal\Core\Routing\MatchingRouteNotFoundException; +use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Url; use Drupal\globalredirect\RedirectChecker; +use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -84,8 +86,10 @@ class GlobalredirectSubscriber implements EventSubscriberInterface { * The redirect checker service. * @param \Symfony\Component\Routing\RequestContext * Request context. + * @param \Drupal\Core\Routing\UrlGeneratorInterface + * Url generator. */ - public function __construct(ConfigFactoryInterface $config_factory, AliasManager $alias_manager, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager, RedirectChecker $redirect_checker, RequestContext $context) { + public function __construct(ConfigFactoryInterface $config_factory, AliasManager $alias_manager, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager, RedirectChecker $redirect_checker, RequestContext $context,UrlGeneratorInterface $urlGenerator) { $this->config = $config_factory->get('globalredirect.settings'); $this->aliasManager = $alias_manager; $this->languageManager = $language_manager; @@ -93,6 +97,7 @@ class GlobalredirectSubscriber implements EventSubscriberInterface { $this->entityManager = $entity_manager; $this->redirectChecker = $redirect_checker; $this->context = $context; + $this->urlGenerator = $urlGenerator; } /** @@ -170,7 +175,7 @@ class GlobalredirectSubscriber implements EventSubscriberInterface { } $system_path = $this->aliasManager->getPathByAlias($path); - $alias = $this->aliasManager->getAliasByPath($system_path, $this->languageManager->getCurrentLanguage()->id); + $alias = $this->aliasManager->getAliasByPath($system_path, $this->languageManager->getCurrentLanguage()->getId()); // If the alias defined in the system is not the same as the one via which // the page has been accessed do a redirect to the one defined in the // system. @@ -206,20 +211,27 @@ class GlobalredirectSubscriber implements EventSubscriberInterface { * The path where we want to redirect. */ protected function setResponse(GetResponseEvent $event, $path) { - if (empty($path) || $path == '/') { - $path = ''; + if (empty($path) || $path == '/' || $path == '') { + $path = $route_name = ''; + } + else { + try { + $result = \Drupal::service('router')->match('/' . $path); + } + catch (ResourceNotFoundException $e) { + throw new MatchingRouteNotFoundException(sprintf('No matching route could be found for the path "%s"', $path), 0, $e); + } + $route_name = $result[RouteObjectInterface::ROUTE_NAME]; } $request = $event->getRequest(); $this->context->fromRequest($request); - $url = Url::createFromPath($path); - parse_str($request->getQueryString(), $query); - $url->setOption('query', $query); - $url->setAbsolute(TRUE); - - if ($this->redirectChecker->canRedirect($url->getRouteName(), $request)) { - $event->setResponse(new RedirectResponse($url->toString(), 301)); + if ($this->redirectChecker->canRedirect($route_name, $request)) { + parse_str($request->getQueryString(), $query); + // @todo convert to Url::fromUri but need to deal with aliasing. + $url = $this->urlGenerator->generateFromPath($path, array('query' => $query, 'absolute' => TRUE)); + $event->setResponse(new RedirectResponse($url, 301)); } }