You are here

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

Expanded class hierarchy of TrailingSlashUrlGenerator

File

src/Routing/TrailingSlashUrlGenerator.php, line 20

Namespace

Drupal\trailing_slash\Routing
View 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

Namesort descending Modifiers Type Description Overrides
TrailingSlashUrlGenerator::$pathProcessorLanguage private property
TrailingSlashUrlGenerator::fixFrontMultilingualUrl private function
TrailingSlashUrlGenerator::generateFromRoute public function Generates a URL or path for a specific route based on the given parameters. Overrides UrlGenerator::generateFromRoute
TrailingSlashUrlGenerator::getLanguagePrefix private function
TrailingSlashUrlGenerator::__construct public function Overrides UrlGenerator::__construct
UrlGenerator::$context protected property
UrlGenerator::$decodedChars protected property Overrides characters that will not be percent-encoded in the path segment.
UrlGenerator::$pathProcessor protected property The path processor to convert the system path to one suitable for urls.
UrlGenerator::$provider protected property The route provider.
UrlGenerator::$requestStack protected property A request stack object.
UrlGenerator::$routeProcessor protected property The route processor.
UrlGenerator::doGenerate protected function Substitute the route parameters into the route path.
UrlGenerator::generate public function Generates a URL or path for a specific route based on the given parameters.
UrlGenerator::getContext public function Gets the request context. 1
UrlGenerator::getInternalPathFromRoute protected function Gets the path of a route. 1
UrlGenerator::getPathFromRoute public function Gets the internal path (system path) for a route. Overrides UrlGeneratorInterface::getPathFromRoute
UrlGenerator::getRoute protected function Find the route using the provided route name. 1
UrlGenerator::getRouteDebugMessage public function Convert a route identifier (name, content object etc) into a string usable for logging and other debug/error messages
UrlGenerator::isStrictRequirements public function
UrlGenerator::processPath protected function Passes the path to a processor manager to allow alterations. 1
UrlGenerator::processRoute protected function Passes the route to the processor manager for altering before compilation. 1
UrlGenerator::setContext public function Sets the request context. 1
UrlGenerator::setStrictRequirements public function
UrlGenerator::supports public function Whether this generator supports the supplied $name.