class TrailingSlashUrlGenerator in Trailing Slash 8
Generates URLs from route names and parameters.
We extend the Urls generation service to add trailing slash to the Urls that correspond to <front> in multilanguage that by default is removed after the processOutbound.
Hierarchy
- class \Drupal\Core\Routing\UrlGenerator implements UrlGeneratorInterface
- class \Drupal\trailing_slash\Routing\TrailingSlashUrlGenerator
Expanded class hierarchy of TrailingSlashUrlGenerator
File
- src/
Routing/ TrailingSlashUrlGenerator.php, line 20
Namespace
Drupal\trailing_slash\RoutingView source
class TrailingSlashUrlGenerator extends UrlGenerator {
/**
* @var \Drupal\language\HttpKernel\PathProcessorLanguage
*/
private $pathProcessorLanguage;
/**
* @param \Drupal\Core\Routing\RouteProviderInterface $provider
* @param \Drupal\Core\PathProcessor\OutboundPathProcessorInterface $path_processor
* @param \Drupal\Core\RouteProcessor\OutboundRouteProcessorInterface $route_processor
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* @param array $filter_protocols
* @param \Drupal\language\HttpKernel\PathProcessorLanguage $path_processor_language
*/
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;
}
/**
* {@inheritdoc}
*/
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;
}
/**
* @param $route_name
* @param $options
*
* @return string
*/
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'] : '';
}
/**
* @param $url
*
* @return string
*/
private function fixFrontMultilingualUrl($url) {
TrailingSlashHelper::add($url);
return $url;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TrailingSlashUrlGenerator:: |
private | property | ||
TrailingSlashUrlGenerator:: |
private | function | ||
TrailingSlashUrlGenerator:: |
public | function |
Generates a URL or path for a specific route based on the given parameters. Overrides UrlGenerator:: |
|
TrailingSlashUrlGenerator:: |
private | function | ||
TrailingSlashUrlGenerator:: |
public | function |
Overrides UrlGenerator:: |
|
UrlGenerator:: |
protected | property | ||
UrlGenerator:: |
protected | property | Overrides characters that will not be percent-encoded in the path segment. | |
UrlGenerator:: |
protected | property | The path processor to convert the system path to one suitable for urls. | |
UrlGenerator:: |
protected | property | The route provider. | |
UrlGenerator:: |
protected | property | A request stack object. | |
UrlGenerator:: |
protected | property | The route processor. | |
UrlGenerator:: |
protected | function | Substitute the route parameters into the route path. | |
UrlGenerator:: |
public | function | Generates a URL or path for a specific route based on the given parameters. | |
UrlGenerator:: |
public | function | Gets the request context. | 1 |
UrlGenerator:: |
protected | function | Gets the path of a route. | 1 |
UrlGenerator:: |
public | function |
Gets the internal path (system path) for a route. Overrides UrlGeneratorInterface:: |
|
UrlGenerator:: |
protected | function | Find the route using the provided route name. | 1 |
UrlGenerator:: |
public | function | Convert a route identifier (name, content object etc) into a string usable for logging and other debug/error messages | |
UrlGenerator:: |
public | function | ||
UrlGenerator:: |
protected | function | Passes the path to a processor manager to allow alterations. | 1 |
UrlGenerator:: |
protected | function | Passes the route to the processor manager for altering before compilation. | 1 |
UrlGenerator:: |
public | function | Sets the request context. | 1 |
UrlGenerator:: |
public | function | ||
UrlGenerator:: |
public | function | Whether this generator supports the supplied $name. |