You are here

protected function ViewsUrlGenerator::cleanRouteParameters in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 modules/simple_sitemap_views/src/Plugin/simple_sitemap/UrlGenerator/ViewsUrlGenerator.php \Drupal\simple_sitemap_views\Plugin\simple_sitemap\UrlGenerator\ViewsUrlGenerator::cleanRouteParameters()

Clears the URL from parameters that are not present in the arguments.

Parameters

\Drupal\Core\Url $url: The URL object.

array $args: Array of arguments.

Throws

\UnexpectedValueException. If this is a URI with no corresponding route.

1 call to ViewsUrlGenerator::cleanRouteParameters()
ViewsUrlGenerator::processDataSet in modules/simple_sitemap_views/src/Plugin/simple_sitemap/UrlGenerator/ViewsUrlGenerator.php

File

modules/simple_sitemap_views/src/Plugin/simple_sitemap/UrlGenerator/ViewsUrlGenerator.php, line 257

Class

ViewsUrlGenerator
Views URL generator plugin.

Namespace

Drupal\simple_sitemap_views\Plugin\simple_sitemap\UrlGenerator

Code

protected function cleanRouteParameters(Url $url, array $args) : void {
  $parameters = $url
    ->getRouteParameters();

  // Check that the number of params does not match the number of arguments.
  if (count($parameters) != count($args)) {
    $route_name = $url
      ->getRouteName();
    $route = $this->routeProvider
      ->getRouteByName($route_name);
    $variables = $route
      ->compile()
      ->getVariables();

    // Remove params that are not present in the arguments.
    foreach ($variables as $variable_name) {
      if (empty($args)) {
        unset($parameters[$variable_name]);
      }
      else {
        array_shift($args);
      }
    }

    // Set new route params.
    $url
      ->setRouteParameters($parameters);
  }
}