You are here

protected function ViewsUrlGenerator::cleanRouteParameters in Simple XML sitemap (Views integration) 8

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 src/Plugin/simple_sitemap/UrlGenerator/ViewsUrlGenerator.php

File

src/Plugin/simple_sitemap/UrlGenerator/ViewsUrlGenerator.php, line 280
Contains Views URL generator.

Class

ViewsUrlGenerator
Views URL generator plugin.

Namespace

Drupal\simple_sitemap_views\Plugin\simple_sitemap\UrlGenerator

Code

protected function cleanRouteParameters(Url $url, array $args) {
  $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);
  }
}