TrailingSlashUrlGenerator.php in Trailing Slash 8
File
src/Routing/TrailingSlashUrlGenerator.php
View source
<?php
namespace Drupal\trailing_slash\Routing;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\RouteProcessor\OutboundRouteProcessorInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Routing\UrlGenerator;
use Drupal\trailing_slash\Helper\Url\TrailingSlashHelper;
use Drupal\language\HttpKernel\PathProcessorLanguage;
use Symfony\Component\HttpFoundation\RequestStack;
class TrailingSlashUrlGenerator extends UrlGenerator {
private $pathProcessorLanguage;
public function __construct(RouteProviderInterface $provider, OutboundPathProcessorInterface $path_processor, OutboundRouteProcessorInterface $route_processor, RequestStack $request_stack, array $filter_protocols = [
'http',
'https',
], PathProcessorLanguage $path_processor_language) {
parent::__construct($provider, $path_processor, $route_processor, $request_stack, $filter_protocols);
$this->pathProcessorLanguage = $path_processor_language;
}
public function generateFromRoute($name, $parameters = [], $options = [], $collect_bubbleable_metadata = FALSE) {
$url = parent::generateFromRoute($name, $parameters, $options, $collect_bubbleable_metadata);
if (\Drupal::languageManager()
->isMultilingual() && $name === '<front>' && $this
->getLanguagePrefix($name, $options)) {
if ($collect_bubbleable_metadata) {
$url
->setGeneratedUrl($this
->fixFrontMultilingualUrl($url
->getGeneratedUrl()));
}
else {
$url = $this
->fixFrontMultilingualUrl($url);
}
}
return $url;
}
private function getLanguagePrefix($route_name, $options) {
$language_options = $options;
$this->pathProcessorLanguage
->processOutbound($route_name, $language_options, $this->requestStack
->getCurrentRequest());
return !empty($language_options['prefix']) ? $language_options['prefix'] : '';
}
private function fixFrontMultilingualUrl($url) {
TrailingSlashHelper::add($url);
return $url;
}
}